【问题标题】:Android Maps v2 : add multiple markers to google mapfragmentAndroid Maps v2:向谷歌地图片段添加多个标记
【发布时间】:2016-04-28 15:45:01
【问题描述】:

我正在尝试使用以下代码向谷歌地图添加 2 个标记。它只显示一个位置而不是两个。任何人都可以看看它并发表评论吗?我看到位置值与调试器不同。

public void updateMapWithNewLocation() {

    Marker marker1 = null;
    Marker marker2 = null;

    LatLng latLng1 = null;
    LatLng latLng2 = null;

    if (mMyLocation != null) {

        latLng1 = new LatLng(mMyLocation.getLatitude(), mMyLocation.getLongitude());

        MarkerOptions myMarkerOptions = new MarkerOptions()
                .position(latLng1)
                .title("me");
        marker1 = mMap.addMarker(myMarkerOptions);
    }

    if (mFriendLocation != null) {
        latLng2 = new LatLng(mMyLocation.getLatitude(), mMyLocation.getLongitude());

        MarkerOptions friendMarkerOptions = new MarkerOptions()
                .position(latLng2)
                .title("friend");
        marker2 = mMap.addMarker(friendMarkerOptions);
    }


    List<Marker> markerList = new ArrayList<>();
    if(marker1 != null){
        markerList.add(marker1);
    }
    if(marker2 != null) {
        markerList.add(marker2);
    }

    zoomToShowAllMarkers(markerList);
}

private void zoomToShowAllMarkers(List<Marker> markers) {
    if ( markers == null || markers.size() < 1)
        return;

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (Marker marker : markers) {
        builder.include(marker.getPosition());
    }

    for (Marker m : markers) {
        builder.include(m.getPosition());
    }
    LatLngBounds bounds = builder.build();

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(bounds.getCenter(), 10));
}

【问题讨论】:

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


    【解决方案1】:

    尝试使用下面的代码。

    // Creating an instance of MarkerOptions
    MarkerOptions markerOptions1 = new MarkerOptions();                    
    
    // Setting latitude and longitude for the marker
    markerOptions1.position(LatLng1);
    
    // Adding marker on the Google Map
    googleMap.addMarker(markerOptions1);    
    
    // Creating an instance of MarkerOptions
    MarkerOptions markerOptions2 = new MarkerOptions();                    
    
    // Setting latitude and longitude for the marker
    markerOptions2.position(LatLng2);
    
    // Adding marker on the Google Map
    googleMap.addMarker(markerOptions2);
    

    或者您可以在自定义方法中添加 using 循环。

    【讨论】:

    • 这与问题中的代码有何不同?
    • 我在父布局中给出了静态高度,所有子视图的高度都与父布局相同
    【解决方案2】:

    这就是我使用边界缩放的方式:

    map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                        @Override
                        public void onMapLoaded() {
                            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 20);
                            map.animateCamera(cameraUpdate);
                        }
                    });
    

    onMapLoaded() 在地图加载后完成缩放。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      感谢您的建议和关注。

      我在使用 myLocation 而不是friendLocation时遇到了复制/粘贴错误

      这一行:

      latLng2 = new LatLng(mMyLocation.getLatitude() mMyLocation.getLongitude());

      抱歉给您添麻烦了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-07
        • 1970-01-01
        相关资源
        最近更新 更多