如何使用 Google 地图绘制点折线 - Android
How to draw dotted polylines using Google Maps - Android
我注意到 iOS 有一个内置函数可以使用 Google 地图绘制点折线,但我找不到 Android 的等效函数。这个想法是 iOS 可以设置两种颜色,一种是线条的颜色,另一种是(透明)之间的 space ,这将创建虚线。 iOS 实现看起来像这样
- (GMSPolyline )drawDashPath:(GMSPath ) path{
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.map = self.googleMapView;
polyline.strokeWidth = 5.0;
NSArray *styles = @[[GMSStrokeStyle solidColor:[UIColor blueColor]],[GMSStrokeStyle solidColor:[UIColor clearColor]]];
NSArray *lengths = @[@5, @5];
polyline.spans = GMSStyleSpans(polyline.path, styles, lengths, kGMSLengthRhumb);
return polyline;
}
对于Android,我已经实现了虚线,但我必须使用计算和操作来完成。这是我为 Android
所做的
// calculate the difference in latitude and longitude between start and end points
double difLat = latLngDest.latitude - latLngOrig.latitude;
double difLng = latLngDest.longitude - latLngOrig.longitude;
// retrieve current zoom level
double zoom = mMap.getCameraPosition().zoom;
// come up with distance between dotted lines that adjusts according to zoom levels
double divLat = difLat / (zoom * 1.2);
double divLng = difLng / (zoom * 1.2);
LatLng tmpLatOri = latLngOrig;
// add polyline to list
for (int i = 0; i < (zoom * 1.2); i++) {
LatLng loopLatLng = tmpLatOri;
if (i > 0) {
loopLatLng = new LatLng(tmpLatOri.latitude + (divLat * 0.40f), tmpLatOri.longitude + (divLng * 0.40f));
}
polylineList.add(mMap.addPolyline(new PolylineOptions()
.add(loopLatLng)
.add(new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng))
.color(Utils.getColor(mContext, R.color.orange))
.width(18f)));
tmpLatOri = new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng);
}
这看起来不错,但如果存在内置函数,我宁愿使用它。我查看了文档,但找不到 Android.
的任何内容
它在 Android 上还不存在。
2017 年 2 月,Google 在 Google 地图 Android API v2 中发布了一组新的折线和多边形自定义项。特别是您现在可以创建虚线和点线多段线。
在折线和多边形教程的 Shapes Guide. See an example 中查看有关新功能的信息。
您也可以在这里阅读相应的博客post:
https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html
我注意到 iOS 有一个内置函数可以使用 Google 地图绘制点折线,但我找不到 Android 的等效函数。这个想法是 iOS 可以设置两种颜色,一种是线条的颜色,另一种是(透明)之间的 space ,这将创建虚线。 iOS 实现看起来像这样
- (GMSPolyline )drawDashPath:(GMSPath ) path{
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.map = self.googleMapView;
polyline.strokeWidth = 5.0;
NSArray *styles = @[[GMSStrokeStyle solidColor:[UIColor blueColor]],[GMSStrokeStyle solidColor:[UIColor clearColor]]];
NSArray *lengths = @[@5, @5];
polyline.spans = GMSStyleSpans(polyline.path, styles, lengths, kGMSLengthRhumb);
return polyline;
}
对于Android,我已经实现了虚线,但我必须使用计算和操作来完成。这是我为 Android
所做的 // calculate the difference in latitude and longitude between start and end points
double difLat = latLngDest.latitude - latLngOrig.latitude;
double difLng = latLngDest.longitude - latLngOrig.longitude;
// retrieve current zoom level
double zoom = mMap.getCameraPosition().zoom;
// come up with distance between dotted lines that adjusts according to zoom levels
double divLat = difLat / (zoom * 1.2);
double divLng = difLng / (zoom * 1.2);
LatLng tmpLatOri = latLngOrig;
// add polyline to list
for (int i = 0; i < (zoom * 1.2); i++) {
LatLng loopLatLng = tmpLatOri;
if (i > 0) {
loopLatLng = new LatLng(tmpLatOri.latitude + (divLat * 0.40f), tmpLatOri.longitude + (divLng * 0.40f));
}
polylineList.add(mMap.addPolyline(new PolylineOptions()
.add(loopLatLng)
.add(new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng))
.color(Utils.getColor(mContext, R.color.orange))
.width(18f)));
tmpLatOri = new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng);
}
这看起来不错,但如果存在内置函数,我宁愿使用它。我查看了文档,但找不到 Android.
的任何内容它在 Android 上还不存在。
2017 年 2 月,Google 在 Google 地图 Android API v2 中发布了一组新的折线和多边形自定义项。特别是您现在可以创建虚线和点线多段线。
在折线和多边形教程的 Shapes Guide. See an example 中查看有关新功能的信息。
您也可以在这里阅读相应的博客post:
https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html