我通过看折线的文件 ,也没有选择,使其破灭。
难道有人知道如何画虚线的折线与Android谷歌地图SDK V2?
我通过看折线的文件 ,也没有选择,使其破灭。
难道有人知道如何画虚线的折线与Android谷歌地图SDK V2?
现在折线,您可以设置模式是破折号,点或间隙简单地套用以下
public static final int PATTERN_DASH_LENGTH_PX = 20;
public static final int PATTERN_GAP_LENGTH_PX = 20;
public static final PatternItem DOT = new Dot();
public static final PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);
public static final PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);
public static final List<PatternItem> PATTERN_POLYGON_ALPHA = Arrays.asList(GAP, DASH);
private void drawDashedLeg(GoogleMap googleMap, Route route) {
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(ContextCompat.getColor(getContext(), R.color.coolgrey));
polyOptions.addAll(route.getPoints());
polyOptions.pattern(PATTERN_POLYGON_ALPHA);
Polyline polyline = googleMap.addPolyline(polyOptions);
polylines.add(polyline);
}
阿列克谢,我刚刚创建了一个为我工作的功能,我认为这将帮助你:
public static void createDashedLine(GoogleMap map, LatLng latLngOrig, LatLng latLngDest, int color){
double difLat = latLngDest.latitude - latLngOrig.latitude;
double difLng = latLngDest.longitude - latLngOrig.longitude;
double zoom = map.getCameraPosition().zoom;
double divLat = difLat / (zoom * 2);
double divLng = difLng / (zoom * 2);
LatLng tmpLatOri = latLngOrig;
for(int i = 0; i < (zoom * 2); i++){
LatLng loopLatLng = tmpLatOri;
if(i > 0){
loopLatLng = new LatLng(tmpLatOri.latitude + (divLat * 0.25f), tmpLatOri.longitude + (divLng * 0.25f));
}
Polyline polyline = map.addPolyline(new PolylineOptions()
.add(loopLatLng)
.add(new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng))
.color(color)
.width(5f));
tmpLatOri = new LatLng(tmpLatOri.latitude + divLat, tmpLatOri.longitude + divLng);
}
}
这是不可能的当前版本。 按照此问题的更新: https://code.google.com/p/gmaps-api-issues/issues/detail?id=4633
UPDATE
最近,谷歌实现这一功能在谷歌地图API的Android v2的折线和标记问题4633为固定。
看到有关中风模式的信息形状指南 。 见一个例子中折线和多边形的教程。
您还可以在这里阅读相应的博客文章:
https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html
我创建了下面的函数来绘制折线点缀用经纬度点的列表。 这个算法创建的0.002公里(接着0.002公里米间隙)而不管缩放线。 当你不想再情节折线变焦变化时,这是非常有用的。
private void drawDashedPolyLine(GoogleMap mMap, ArrayList<LatLng> listOfPoints, int color) {
/* Boolean to control drawing alternate lines */
boolean added = false;
for (int i = 0; i < listOfPoints.size() - 1 ; i++) {
/* Get distance between current and next point */
double distance = getConvertedDistance(listOfPoints.get(i),listOfPoints.get(i + 1));
/* If distance is less than 0.002 kms */
if (distance < 0.002) {
if (!added) {
mMap.addPolyline(new PolylineOptions()
.add(listOfPoints.get(i))
.add(listOfPoints.get(i + 1))
.color(color));
added = true;
} else {/* Skip this piece */
added = false;
}
} else {
/* Get how many divisions to make of this line */
int countOfDivisions = (int) ((distance/0.002));
/* Get difference to add per lat/lng */
double latdiff = (listOfPoints.get(i+1).latitude - listOfPoints
.get(i).latitude) / countOfDivisions;
double lngdiff = (listOfPoints.get(i + 1).longitude - listOfPoints
.get(i).longitude) / countOfDivisions;
/* Last known indicates start point of polyline. Initialized to ith point */
LatLng lastKnowLatLng = new LatLng(listOfPoints.get(i).latitude, listOfPoints.get(i).longitude);
for (int j = 0; j < countOfDivisions; j++) {
/* Next point is point + diff */
LatLng nextLatLng = new LatLng(lastKnowLatLng.latitude + latdiff, lastKnowLatLng.longitude + lngdiff);
if (!added) {
mMap.addPolyline(new PolylineOptions()
.add(lastKnowLatLng)
.add(nextLatLng)
.color(color));
added = true;
} else {
added = false;
}
lastKnowLatLng = nextLatLng;
}
}
}
}
private double getConvertedDistance(LatLng latlng1, LatLng latlng2) {
double distance = DistanceUtil.distance(latlng1.latitude,
latlng1.longitude,
latlng2.latitude,
latlng2.longitude);
BigDecimal bd = new BigDecimal(distance);
BigDecimal res = bd.setScale(3, RoundingMode.DOWN);
return res.doubleValue();
}
的Util类来计算两个经纬度之间的距离:
public class DistanceUtil {
public static double distance(double lat1, double lon1, double lat2,
double lon2) {
if ((lat1 == lat2) && (lon1 == lon2)) {
return 0;
} else
return distance(lat1, lon1, lat2, lon2, 'K');
}
public static double distance(double lat1, double lon1, double lat2,
double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private static double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
}
注:上述算法产生非常大的数字,其可能需要一些时间来呈现折线。 它是有用的,只有当点的名单是小的。