Har*_*hta 15 android polyline marker google-maps-android-api-2
我有一个随机纬度和经度点的列表,我正在他们之间绘制一条路线.我的问题是如何在谷歌地图中绑定这条路线,我在实用方法下面做了
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());*/
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里最后一次暂停是布尔值,表示它是否是用于指示红色折线的暂停点.
但它没有用.感谢任何帮助.
Ris*_*rma 23
尝试以这种方式实现
/**
* 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));
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
查看图像后,地图上方有布局.所以它需要你设置Variable Padding.你可以这样做
googleMap.setPadding(left, top, right, bottom);
Run Code Online (Sandbox Code Playgroud)
注意:设置变量填充时,可以初始化routePadding = 0; 根据你的情况,近似值:left = right = 10,bottom=100,top=200.一旦设置,您可以根据您的要求校准'.
Recommendation: You can calculate the height of those (top and bottom) layouts in pixels and then set padding accordingly.
为什么放大不起作用的原因可能是因为在调用方法时map尚未膨胀:
moveCamera(com.google.android.gms.maps.CameraUpdate)
尝试添加ViewTreeObserver.OnGlobalLayoutListener到地图:
ViewTreeObserver vto = googleMap.getViewTreeObserver();
ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
googleMap.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
googleMap.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
}
};
vto.addOnGlobalLayoutListener(globalLayoutListener);
Run Code Online (Sandbox Code Playgroud)
如果上面的方法不起作用GoogleMap,它有自己的布局监听器,你可以使用:
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
googleMap.setOnCameraChangeListener(null);
}
});
Run Code Online (Sandbox Code Playgroud)
但是,从play-services-maps 9.4.0版本的API开始,不推荐使用上述方法.使用以下之一:
| 归档时间: |
|
| 查看次数: |
6364 次 |
| 最近记录: |