2017-07-12 2 views
0

Ich möchte mehrere Pfade zwischen verschiedenen Punkten in Google Maps zeigen.Ich benutze unten Code.Ich habe auch Bilder hier angehängt.So zeigen Sie die verschiedenen Pfade zwischen verschiedenen Punkten in Google Maps in Android

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{ 
     private GoogleMap mMap; 


      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_maps); 
       // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
       SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
         .findFragmentById(R.id.map); 
       mapFragment.getMapAsync(this); 
      } 


     public void onMapReady(GoogleMap googleMap) { 
       mMap = googleMap; 

       // Add a marker in Sydney and move the camera 
       LatLng Madhapur = new LatLng(17, 78); 
       mMap.addMarker(new MarkerOptions().position(Madhapur)); 
       LatLng CyberTowers=new LatLng(17.4504,78.3811); 
       mMap.addMarker(new MarkerOptions().position(CyberTowers)); 
       LatLng Kondapur=new LatLng(17.4622,78.3568); 
       mMap.addMarker(new MarkerOptions().position(Kondapur)); 
       mMap.addPolyline(new PolylineOptions(). 
         add(Madhapur, 
           new LatLng(17.4504,78.3811), 
           new LatLng(17.4524, 78.3796), 
           Kondapur 
         ) 
         .width(10) 
         .color(Color.BLUE)); 
     } 
} 

Image attached to show exact requirement

+0

Können Sie mehr erklären? Was meinst du mit "multiple path"? Zum Beispiel haben Sie 3 Standorte A B C. Sie möchten eine Linie von A nach B, B nach C, A nach C zeichnen? oder möchtest du eine Richtung zeichnen? – GiaLe

+0

Ich möchte die Richtung zwischen mehreren Orten zeichnen.Mehrere Pfad bedeutet Routenrichtung. – Ratna

+0

Ich habe es, siehe meine Antwort unten – GiaLe

Antwort

0
  1. Bitte setzen Sie sich Daten aus dieser api, mit diesen Parametern: Herkunft ( lat, long der Quelle), Ziel (lat, long Bestimmungsort), Schlüssel ( google api Schlüssel) http://maps.googleapis.com/maps/api/directions/json

  2. Daten, die von oben api erhalten Sie eine Liste der Lage von A nach B Alle Sie tun müssen, ist eine Liste von Punkt aus den Daten zu ziehen. Sie erhalten eine Richtungslinie von a nach b

    endgültig JSONObject json = new JSONObject (result); JSONArray routeArray = json.getJSONArray ("routes"); JSON-Objekt routes = routeArray.getJSONObject (0); JSONObject overviewPolylines = Routen .getJSONObject ("overview_polyline"); Zeichenfolge encodedString = overviewPolylines.getString ("points");

    JSONArray legArray =routes.getJSONArray("legs"); 
    JSONObject legs= legArray.getJSONObject(0); 
    JSONObject distance =legs.getJSONObject("distance"); 
    String dis=distance.getString("text"); 
    RouteActivity.txtDistance.setText("("+dis+")"); 
    
    JSONObject duration =legs.getJSONObject("duration"); 
    String dur=duration.getString("text"); 
    RouteActivity.txtTime.setText(dur); 
    
    List<LatLng> list = decodePoly(encodedString); 
    
    PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true); 
    for (int z = 0; z < list.size(); z++) { 
        LatLng point = list.get(z); 
        options.add(point); 
    } 
    mGoogleMap.addPolyline(options); 
    

private static Liste decodePoly (String codiert) {

List<LatLng> poly = new ArrayList<LatLng>(); 
int index = 0, len = encoded.length(); 
int lat = 0, lng = 0; 

while (index < len) { 
    int b, shift = 0, result = 0; 
    do { 
     b = encoded.charAt(index++) - 63; 
     result |= (b & 0x1f) << shift; 
     shift += 5; 
    } while (b >= 0x20); 
    int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
    lat += dlat; 

    shift = 0; 
    result = 0; 
    do { 
     b = encoded.charAt(index++) - 63; 
     result |= (b & 0x1f) << shift; 
     shift += 5; 
    } while (b >= 0x20); 
    int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
    lng += dlng; 

    LatLng p = new LatLng((((double) lat/1E5)), 
      (((double) lng/1E5))); 
    poly.add(p); 
} 

return poly; 

}

Verwandte Themen