我的地图上有 2 个标记。如何将相机聚焦在两者上而不只是将相机移动到其中一个?
I have 2 marker on my map. How to focus camera on both and not just move camera to one of them?
我已经在我的应用程序中实现了 Google 地图,并在上面添加了 2 个标记。
方法如下:
LatLng mainUserLocation = new LatLng(Double.valueOf(currentLt), Double.valueOf(currentLn));
mMap.addMarker(new MarkerOptions().position(mainUserLocation).title("You"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(mainUserLocation))
mMap.animateCamera(CameraUpdateFactory.zoomTo(20), 2000, null);
LatLng otherPlayersLocation = new LatLng(currentLtAU, currentLnAU);
mMap.addMarker(new MarkerOptions().position(otherPlayersLocation).title(nameAU));
问题是摄像头正在缩放并聚焦在一个标记上,而另一个标记却看不见了!
我希望两个标记或所有标记都在视线范围内。如何实现?
请告诉我。
第一种方式
您应该使用 CameraUpdate class 进行(可能)所有程序化地图移动。
为此,首先像这样计算所有标记的边界:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for each (Marker m : markers) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
然后通过工厂获取运动描述对象:CameraUpdateFactory:
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
最后移动地图:
googleMap.moveCamera(cu);
或者如果你想要一个动画:
googleMap.animateCamera(cu);
另一种方式
求所有经纬度的平均值,即
double avg_Lat = 23.521252;
double avg_Lng = 72.521252;
然后创建一个新的 Latlng 对象并分配它
LatLng latLng = null;
latLng = new LatLng(avg_Lat, avg_Lng);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12));
它将包括所有经纬度
所以这个问题已经回答了 here 再不提将违反良好的社区道德。
Still, here I will try to break it down in your context. So that you
understand better what steps you need to take.
LatLng mainUserLocation = new LatLng(Double.valueOf(currentLt), Double.valueOf(currentLn));
LatLng otherPlayersLocation = new LatLng(currentLtAU, currentLnAU);
public void showMap() {
mMap.clear();
//Create your Markers List
List<Marker> markersList = new ArrayList<Marker>();
Marker youMarker = mMap.addMarker(new MarkerOptions().position(mainUserLocation).title("You"));
Marker playerMarker = mMap.addMarker(new MarkerOptions().position(otherPlayersLocation).title(nameAU));
//Add them to your list
markersList.add(youMarker);
markersList.add(playerMarker);
//get the latLngbuilder from the marker list
builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
//Bounds padding here
int padding = 50;
//Create bounds here
LatLngBounds bounds = builder.build();
//Create camera with bounds
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
//Check map is loaded
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
//animate camera here
mMap.animateCamera(cu);
}
});
}
因此,在您的代码中,您需要设置这样的方法,只需调用 showMap().
希望此信息对您有所帮助。
我已经在我的应用程序中实现了 Google 地图,并在上面添加了 2 个标记。
方法如下:
LatLng mainUserLocation = new LatLng(Double.valueOf(currentLt), Double.valueOf(currentLn));
mMap.addMarker(new MarkerOptions().position(mainUserLocation).title("You"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(mainUserLocation))
mMap.animateCamera(CameraUpdateFactory.zoomTo(20), 2000, null);
LatLng otherPlayersLocation = new LatLng(currentLtAU, currentLnAU);
mMap.addMarker(new MarkerOptions().position(otherPlayersLocation).title(nameAU));
问题是摄像头正在缩放并聚焦在一个标记上,而另一个标记却看不见了!
我希望两个标记或所有标记都在视线范围内。如何实现?
请告诉我。
第一种方式
您应该使用 CameraUpdate class 进行(可能)所有程序化地图移动。
为此,首先像这样计算所有标记的边界:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for each (Marker m : markers) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
然后通过工厂获取运动描述对象:CameraUpdateFactory:
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
最后移动地图:
googleMap.moveCamera(cu);
或者如果你想要一个动画:
googleMap.animateCamera(cu);
另一种方式
求所有经纬度的平均值,即
double avg_Lat = 23.521252;
double avg_Lng = 72.521252;
然后创建一个新的 Latlng 对象并分配它
LatLng latLng = null;
latLng = new LatLng(avg_Lat, avg_Lng);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12));
它将包括所有经纬度
所以这个问题已经回答了 here 再不提将违反良好的社区道德。
Still, here I will try to break it down in your context. So that you understand better what steps you need to take.
LatLng mainUserLocation = new LatLng(Double.valueOf(currentLt), Double.valueOf(currentLn));
LatLng otherPlayersLocation = new LatLng(currentLtAU, currentLnAU);
public void showMap() {
mMap.clear();
//Create your Markers List
List<Marker> markersList = new ArrayList<Marker>();
Marker youMarker = mMap.addMarker(new MarkerOptions().position(mainUserLocation).title("You"));
Marker playerMarker = mMap.addMarker(new MarkerOptions().position(otherPlayersLocation).title(nameAU));
//Add them to your list
markersList.add(youMarker);
markersList.add(playerMarker);
//get the latLngbuilder from the marker list
builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
//Bounds padding here
int padding = 50;
//Create bounds here
LatLngBounds bounds = builder.build();
//Create camera with bounds
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
//Check map is loaded
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
//animate camera here
mMap.animateCamera(cu);
}
});
}
因此,在您的代码中,您需要设置这样的方法,只需调用 showMap().
希望此信息对您有所帮助。