我正在尝试使用以下代码向Google地图添加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));
}
答案 0 :(得分:0)
尝试使用以下代码。
// 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);
或者您可以在自定义方法中添加使用循环。
答案 1 :(得分:0)
这就是我用边界缩放的方法:
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 20);
map.animateCamera(cameraUpdate);
}
});
onMapLoaded()
在地图加载后完成缩放。
希望这有帮助。
答案 2 :(得分:0)
感谢您的建议并查看。
我有使用myLocation而不是friendLocation
的复制/粘贴错误这一特定行:
latLng2 = new LatLng(mMyLocation.getLatitude()mMyLocation.getLongitude()); &lt; - 应该使用mFriendLocation而不是mMyLocation。
不好意思。