Android Story

[ Android ] 다음 맵에서 지원하지 않는 점선 라인.. 해결??

WhiteDuck 2016. 11. 26. 21:50

Draw Dashed Line



다음 맵에서는 점선 그리기가 제공되지 않는다. 그래서 강제로 그린다.


어떻게??

이렇게...


// using in Daum Map API
public class DashedLineMaker {
class Route {
public double latitude;
public double longitude;
}
/**
* 점선 그리기
* @param routeList
*/
public static void drawDashedPolyLine(MapView mapView,ArrayList<Route> routeList , double dashedSize) {
/* Boolean to control drawing alternate lines */
boolean added = false;
for (int i = 0; i < routeList.size() - 1 ; i++) {
/* Get distance between current and next point */
double distance = getConvertedDistance(routeList.get(i),routeList.get(i + 1));
/* If distance is less than 0.002 kms */
if (distance < dashedSize) {
if (!added) {
MapPolyline routeLine = new MapPolyline();
routeLine.setLineColor(Color.argb(255, 83, 144, 245));
routeLine.addPoint(MapPoint.mapPointWithGeoCoord(routeList.get(i).latitude, routeList.get(i).longitude));
routeLine.addPoint(MapPoint.mapPointWithGeoCoord(routeList.get(i + 1).latitude, routeList.get(i + 1).longitude));
mapView.addPolyline(routeLine);
added = true;
} else {
/* Skip this piece */
added = false;
}
} else {
/* Get how many divisions to make of this line */
int countOfDivisions = (int) ((distance / dashedSize));
/* Get difference to add per lat/lng */
double latdiff = (routeList.get(i+1).latitude - routeList.get(i).latitude) / countOfDivisions;
double lngdiff = (routeList.get(i + 1).longitude - routeList.get(i).longitude) / countOfDivisions;
/* Last known indicates start point of polyline. Initialized to ith point */
Route lastKnowLatLng = new Route(routeList.get(i).latitude, routeList.get(i).longitude);
for (int j = 0; j < countOfDivisions; j++) {
/* Next point is point + diff */
Route nextLatLng = new Route(lastKnowLatLng.latitude + latdiff, lastKnowLatLng.longitude + lngdiff);
if (!added) {
MapPolyline routeLine = new MapPolyline();
routeLine.setLineColor(Color.argb(255, 83, 144, 245));
routeLine.addPoint(MapPoint.mapPointWithGeoCoord(lastKnowLatLng.latitude, lastKnowLatLng.longitude));
routeLine.addPoint(MapPoint.mapPointWithGeoCoord(nextLatLng.latitude, nextLatLng.longitude));
mapView.addPolyline(routeLine);
added = true;
} else {
added = false;
}
lastKnowLatLng = nextLatLng;
}
}
}
}
/**
* 두 점 사이의 거리
* @param route1
* @param route2
* @return
*/
public static double getConvertedDistance(Route route1, Route route2) {
double distance = distance(
route1.latitude,
route1.longitude,
route2.latitude,
route2.longitude);
BigDecimal bd = new BigDecimal(distance);
BigDecimal res = bd.setScale(3, RoundingMode.DOWN);
return res.doubleValue();
}
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);
}
}




stackoverflow에서 참조를 했는데 어디서 했는지 모르겠다.


일단 글을 남긴다....


아! 적용해보니 확대를 많이하면 조금씩 엇나가 있는게 보인다. 정확한 점선은 아니지만


퀼리티가 그리 중요하지 않다면 써보는 것을 추천.

반응형