[英]Is there any limit to drawing path using Geocoder (Android)?
我能夠在兩個位置之間繪制一條路徑,但如果距離太長 - 超過300公里 - 則路徑未完全繪制。
我正在使用下面的代碼來繪制路徑:
class MapOverlay extends com.google.android.maps.Overlay {
Road mRoad;
ArrayList<GeoPoint> mPoints;
public MapOverlay(Road road, MapView mv) {
mRoad = road;
if (road.mRoute.length > 0) {
mPoints = new ArrayList<GeoPoint>();
for (int i = 0; i < road.mRoute.length; i++) {
mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
(int) (road.mRoute[i][0] * 1000000)));
}
int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
.getLatitudeE6()) / 2);
int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
.getLongitudeE6()) / 2);
GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);
MapController mapController = mv.getController();
mapController.animateTo(moveTo);
mapController.setZoom(8);
}
}
@Override
public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
super.draw(canvas, mv, shadow);
drawPath(mv, canvas);
return true;
}
public void drawPath(MapView mv, Canvas canvas) {
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
for (int i = 0; i < mPoints.size(); i++) {
Point point = new Point();
mv.getProjection().toPixels(mPoints.get(i), point);
x2 = point.x;
y2 = point.y;
if (i > 0) {
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}
}
首先,您應該使用Google Maps API V2而不是舊的已棄用的V1 API
在“活動”中,創建Google地圖和折線參考:
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback{
private GoogleMap mMap;
Polyline line;
//......
首先定義您的航點列表:
List<LatLng> latLngWaypointList = new ArrayList<>();
獲取您的路線,繪制路線的折線,然后繪制航路點標記:
class GetDirectionsAsync extends AsyncTask<LatLng, Void, List<LatLng>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<LatLng> doInBackground(LatLng... params) {
List<LatLng> route = new ArrayList<>();
//populate route......
return route;
}
@Override
protected void onPostExecute(List<LatLng> route) {
if (route == null) return;
if (line != null){
line.remove();
}
PolylineOptions options = new PolylineOptions().width(5).color(Color.MAGENTA).geodesic(true);
for (int i = 0; i < pointsList.size(); i++) {
LatLng point = route.get(i);
//If this point is a waypoint, add it to the list
if (isWaypoint(i)) {
latLngWaypointList.add(point);
}
options.add(point);
}
//draw the route:
line = mMap.addPolyline(options);
//draw waypoint markers:
for (LatLng waypoint : latLngWaypointList) {
mMap.addMarker(new MarkerOptions().position(new LatLng(waypoint.latitude, waypoint.longitude))
.title("Waypoint").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)));
}
}
}
這里只是isWaypoint()的占位符實現:
public boolean isWaypoint(int i) {
//replace with your implementation
if (i % 17 == 0) {
return true;
}
return false;
}
從西海岸到東海岸的路線的結果,大約2500英里:
路線較小的結果:
請注意,我也在此示例中使用Google Directions Api,以使路線對齊道路。 有關如何使用Google Directions API的完整示例,請參閱此處: https : //stackoverflow.com/a/32940175/4409409
解
對我有用的解決方案是向android內核顯式請求更多堆內存。 您可以在清單中執行此操作。
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:largeHeap="true" .........
請看看這條線
機器人:largeHeap = “真”
這是您需要在代碼中執行的操作。
希望這個解釋對你的問題有所幫助。
如果我沒有錯在Google地圖中繪制路徑非常簡單。 源代碼
var mumbai = new google.maps.LatLng(18.9750, 72.8258);
var mapOptions = {
zoom: 8,
center: mumbai
};
map = new google.maps.Map(document.getElementById('dmap'), mapOptions);
directionsDisplay.setMap(map);
source="18.9750, 72.8258";
destination= "18.9550, 72.8158";
source = (source1.replace(" ", ","));
destination = (destination1.replace(" ", ","));
var request = {
origin: source,
destination: destination,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.