简体   繁体   中英

Android Google Maps - How to divide Google Maps route in equal segments?

In my custom Google Maps, I draw a route between two locations (say Pune to Mumbai) using this URL

https://maps.googleapis.com/maps/api/directions/json?origin=Pune&destination=Mumbai&sensor=false&mode=driving&alternatives=false

在此输入图像描述

Parsing this response I get number of points between source and destination and store it in ArrayList

ArrayList<LocationModel> listAllPoints = getPoints();

This ArrayList contains around 2600 points.

My question is, how can I divide this route in 10 equal segments, ie total 11 points including source and destination. Like this

| A | B | C | D | E | F | G | H | I | J |

Where | = each point, Character = segment

Distance between A and B, B and C, C and D should be equal.

I should be able to do this by dividing listAllPoints in 11 Parts, but problem is that, points inside listAllPoints are not scattered uniformly.

Any help will be greatly appreciated.

Refer this simple code.

  1. Get Document

      String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + start.latitude + "," + start.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=driving"; HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(in); 
  2. Draw line:

      ArrayList<LatLng> directionPoint = v2GetRouteDirection .getDirection(document); PolylineOptions rectLine = new PolylineOptions().width(10) .color(Color.RED); for (int i = 0; i < directionPoint.size(); i++) { rectLine.add(directionPoint.get(i)); } // Adding route on the map mGoogleMap.addPolyline(rectLine); 
  3. Add Point

      MarkerOptions markerOptions = new MarkerOptions(); Bitmap icon = BitmapFactory.decodeResource( EMaps2.this.getResources(), R.drawable.pointa); markerOptions .icon(BitmapDescriptorFactory.fromBitmap(icon)); markerOptions.position(fromPosition); markerOptions.title("Point A"); mGoogleMap.addMarker(markerOptions); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM