放大特定路线的谷歌地图 [英] zoom over specific route google map
问题描述
我有一个随机的纬度和经度点列表,并且正在它们之间画一条路线.我的问题是如何在实用工具方法下面制作的Google Map中绑定这条路线
I have a list of random latitude and longitude points and I am drawing a route between them. My question is how to bound this route within google map I made below utility method
public static void drawRouteIntoMap(final List<? extends MapHelper> position, final GoogleMap googleMap) {
/*List<MapHelper> position = new ArrayList<MapHelper>();
for (int i = lastPosition; i < maps.size(); i++) {
position.add(maps.get(i));
}*/
if (position.size() > 0 && Validator.isNotNull(googleMap)) {
googleMap.clear();
List<PolylineOptions> polylineOptionses = new ArrayList<PolylineOptions>();
PolylineOptions option = null;
Boolean lastPause = null;
for (MapHelper map : position) {
if (map.isPause()) {
if (Validator.isNull(lastPause) || !lastPause) {
option = new PolylineOptions().width(5).color(Color.rgb(255, 0, 155)).geodesic(true);
polylineOptionses.add(option);
}
option.add(new LatLng(map.getLatitude(), map.getLongitude()));
} else {
if (Validator.isNull(lastPause) || lastPause) {
option = new PolylineOptions().width(5).color(Color.rgb(0, 179, 253)).geodesic(true);
polylineOptionses.add(option);
}
option.add(new LatLng(map.getLatitude(), map.getLongitude()));
}
lastPause = map.isPause();
}
for (PolylineOptions options : polylineOptionses) {
googleMap.addPolyline(options);
}
if(Validator.isNotNull(option)){
//List<LatLng> points = option.getPoints();
final LatLngBounds.Builder mapBounds = new LatLngBounds.Builder();
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
LatLng startPoint = new LatLng(position.get(0).getLatitude(), position.get(0).getLongitude());
googleMap.addMarker(new MarkerOptions().position(startPoint).title("start").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mapBounds.include(startPoint);
LatLng endPoint = new LatLng(position.get(position.size() - 1).getLatitude(), position.get(position.size() - 1).getLongitude());
mapBounds.include(endPoint);
googleMap.addMarker(new MarkerOptions().position(endPoint).title("finish").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
/* googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
googleMap.moveCamera(CameraUpdateFactory.zoomOut());*/
}
});
}
}
}
最后一个暂停是布尔值,表示是否是暂停点,用于指示红色折线.
here last pause is boolean indicating whether it is paused point for indicating red color polyline.
但是它不起作用.感谢您的帮助.
but it is not working.Any help is appreciated.
推荐答案
尝试以这种方式实现
/**
* Zooms a Route (given a List of LalLng) at the greatest possible zoom level.
*
* @param googleMap: instance of GoogleMap
* @param lstLatLngRoute: list of LatLng forming Route
*/
public void zoomRoute(GoogleMap googleMap, List<LatLng> lstLatLngRoute) {
if (googleMap == null || lstLatLngRoute == null || lstLatLngRoute.isEmpty()) return;
LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
for (LatLng latLngPoint : lstLatLngRoute)
boundsBuilder.include(latLngPoint);
int routePadding = 100;
LatLngBounds latLngBounds = boundsBuilder.build();
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, routePadding));
}
更新
查看图像后,地图上方有布局.因此,这需要您设置可变填充.您可以按照以下方式进行操作
After looking at the image, there are layouts above map. So it would require you to set Variable Padding. You can do it as
googleMap.setPadding(left, top, right, bottom);
注意:在设置变量填充时,您可以初始化routePadding = 0
;否则,请执行以下操作.
在您的情况下,近似值为:left = right = 10
,bottom=100
,top=200
.设置好后,您可以根据需要校准它们.
Note: While setting variable padding, you can initialize routePadding = 0
;
In your case, approximations are: left = right = 10
, bottom=100
, top=200
. Once set, you can calibrate'em as per your requirement.
Recommendation: You can calculate the height of those (top and bottom) layouts in pixels and then set padding accordingly.
这篇关于放大特定路线的谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!