Is there any limit to drawing path using Geocoder (Android)?
I am able to draw a path between two locations, but if the distance is too long - more than 300 Kilometers - the path is not drawn completely.
I am using the code below in order to draw the path:
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;
}
}
}
First of all, you should be using Google Maps API V2 instead of the old deprecated V1 API
In the Activity, create the Google Map and Polyline references:
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback{
private GoogleMap mMap;
Polyline line;
//......
First define your waypoint list:
List<LatLng> latLngWaypointList = new ArrayList<>();
Get your route, draw the Polyline for the route, and then draw the waypoint Markers:
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)));
}
}
}
Here is just a placeholder implementation of isWaypoint():
public boolean isWaypoint(int i) {
//replace with your implementation
if (i % 17 == 0) {
return true;
}
return false;
}
Result for a route from the west coast to the east coast, about 2500 miles:
Result of a smaller route:
Note that I'm also using the Google Directions Api in this example in order to make the route snap to the roads. For a full example of how to use the Google Directions api, see here: https://stackoverflow.com/a/32940175/4409409
- I google direction api you don't have a limit for the distance you want the route for. You have other limitations like number of request you can make on 24 hours etc . for details visit here .
- Always use the latest version of google maps api and play services in your android app development.
- Follow the guide in api guide to make you program so that there will be less chances of fault.
- So next you are having problem in drawing distances which is quite larger like 300 km . I assume you can easily draw path for shorter distances and you are having no problem with it. taking this condition into account (that you can do short distance easily) lets begin-
Solution
- The problem you are having in drawing larger path is due to large number of waypoints and which leads to larger requirement of heap memory for the app.
- So the android kernel provides a small memory to app while running for its scratch memory . but that is not enough for the larger routes and your app will hang or crash in worse case.
The solution that worked for me is to explicitly request for more heap memory to the android kernel. You can do this in manifest.
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:largeHeap="true" .........
Please have a look at the line
android:largeHeap="true"
This is what you need to do in your code.
Hope this explanation was helpful for your problem.
If i'm not wrong Its very simple to draw an path in google maps. Source Code
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);
}
});
精彩评论