【问题标题】:Marker not displaying on GoogleMap标记未显示在谷歌地图上
【发布时间】:2015-01-25 08:17:45
【问题描述】:

我正在尝试在GoogleMap 上添加多个标记。这就是我正在做的事情:

private void initilizeMap() {
        try {
            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager()
                        .findFragmentById(R.id.map)).getMap();
                // Enabling MyLocation Layer of Google Map
                googleMap.setMyLocationEnabled(true);

                if (googleMap != null)
                    addMarkers();
                // Getting LocationManager object from System Service
                // LOCATION_SERVICE
                LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

                // Creating a criteria object to retrieve provider
                Criteria criteria = new Criteria();

                // Getting the name of the best provider
                String provider = locationManager.getBestProvider(criteria,
                        true);

                // Getting Current Location
                Location location = locationManager
                        .getLastKnownLocation(provider);

                if (location != null) {
                    onLocationChanged(location);
                }
                locationManager
                        .requestLocationUpdates(provider, 20000, 0, this);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }  

以下函数添加标记:

private void addMarkers() {
        try {
            for (String title : locations.keySet()) {
                if (locations.get(title).getLatitude() != 0
                        && locations.get(title).getLongitude() != 0) {
                    // create marker
                    MarkerOptions marker = new MarkerOptions().position(
                            new LatLng(locations.get(title).getLatitude(),
                                    locations.get(title).getLongitude()))
                            .title(title);
                    marker.icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.pin_map));
                    // adding marker
                    googleMap.addMarker(marker);

                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

但是标记没有显示,尽管它们在我在调试时检查的 GoogleMap 上加起来。也不例外。我尝试更改图标图像,仍然无法正常工作。

【问题讨论】:

  • 您确定您的纬度/经度值在预期区域内吗?如果您之前使用过旧的地图 API,还请注意,LatLng 现在采用双精度值,而不再采用 int 值(与现在预期的双精度值相比,它乘以 1,000000。)
  • @user2808624:正确!!将此作为答案发布。

标签: android google-maps-markers google-maps-android-api-2 android-mapview


【解决方案1】:

您很可能正在使用旧的 int 值作为纬度/经度,因为它们在以前的 GoogleMaps API 的 GeoPoint 中是预期的。这些对于新的 LatLng 对象来说太大了 100 万倍,它需要双精度值而不是整数。

【讨论】:

    猜你喜欢
    • 2021-07-07
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2020-06-08
    相关资源
    最近更新 更多