标记未在Google地图上显示

时间:2014-11-26 20:56:14

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

我正在尝试在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上加起来了。也不例外。我试图更改图标图像,仍然无法正常工作。

1 个答案:

答案 0 :(得分:1)

您最有可能使用旧的int值作为纬度/经度,因为它们是之前GoogleMaps API的GeoPoint所期望的值。对于新的LatLng对象来说,它们的大小是100万倍,它需要双值而不是整数。