【问题标题】:Android Google Map show markers on zoom changeAndroid Google Map 在缩放更改时显示标记
【发布时间】:2014-10-01 15:01:42
【问题描述】:

伙计们,我必须通过缩放在谷歌地图上显示标记。当用户放大或缩小标记时,屏幕上的地图区域将可见。我如何使用 Android 谷歌地图做到这一点?

【问题讨论】:

标签: android google-maps-markers android-mapview android-maps-v2 android-maps


【解决方案1】:

好的,你可以使用 CameraChangeListener:

private HashMap<Integer, Marker> courseMarkers = new HashMap<Integer, Marker>();
//all your visible markers
ArrayList<Item> yourMarkerList = new ArrayList<Item>();
//method to add all your markers with unique ids
addItemsToMap(); //first call to initially show the markers you want

googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

private float currentZoom = -1; //keep track of your current zoom level

@Override
public void onCameraChange(CameraPosition camera) {
    if (camera.zoom != currentZoom){
        currentZoom = camera.zoom;
        //here you will then check your markers
        addItemsToMap(yourMarkerList);
    }
}
});

您将需要一个具有唯一 int id 和 MarkerOptions 变量的类 Item

private void addItemsToMap(List<Item> items)
{
if(this.mMap != null)
{
    //This is the current user-viewable region of the map
    LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;

    //Loop through all the items that are available to be placed on the map
    for(Item m : item) 
    {

        //If the item is within the the bounds of the screen
        if(bounds.contains(item.getMarker().getPosition()))
        {
            //If the item isn't already being displayed
            if(!courseMarkers.containsKey(item.getId()))
            {
                //Add the Marker to the Map and keep track of it with the HashMap
                //getMarkerForItem just returns a MarkerOptions object
                this.courseMarkers.put(item.getId(), this.googleMap.addMarker(item.getMarker())); //getmarkerforitem
            }
        }

        //If the marker is off screen
        else
        {
            //If the course was previously on screen
            if(courseMarkers.containsKey(item.getId()))
            {
                //1. Remove the Marker from the GoogleMap
                courseMarkers.get(item.getId()).remove();

                //2. Remove the reference to the Marker from the HashMap
                courseMarkers.remove(item.getId());
            }
        }
    }
}

}

应该这样做

【讨论】:

  • 我已经在我的代码中完成了这项工作。现在我需要标记方面的帮助我有一个点列表,我需要在放大和缩小时显示/隐藏这些点。我该怎么做?
  • 那么您想在某些标记不在屏幕时隐藏它们吗?
  • 是的,我想要这个并在屏幕上显示某些标记
  • @Even 非常感谢。这里的 courserMarkers 是什么?
  • 看上面的代码,它是一个HashMap,一个将key链接到Object的数据结构
【解决方案2】:
private void pointToPosition(LatLng position) {
    //Build camera position
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(position)
            .zoom(15).build();
    //Zoom in and animate the camera.
    mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-18
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多