2016-07-31 4 views
-5

Gerade jetzt zeigt dieser Code zwei Markierungen an, einen am aktuellen Ort und den anderen am Zielort. Ich brauche die Route zwischen ihnen. Ich habe den Code meiner MapsActivity unten gepostet. Ich weiß, dass wir JSON und Polyline verwenden müssen, um den Job zu erledigen, aber nicht wissen, wo und wie man sie benutzt.Wie zeichne ich die Route zwischen meinem aktuellen Standort und dem Ziel?

MapsActivity.java 
package gcmojo.com.location; 

import android.Manifest; 
import android.content.IntentSender; 
import android.content.pm.PackageManager; 
import android.location.Geocoder; 
import android.location.Location; 
import android.os.AsyncTask; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 


import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.google.android.gms.maps.model.PolylineOptions; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Locale; 

public class MapsActivity extends FragmentActivity implements 
GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener { 
    public static final String TAG = MapsActivity.class.getSimpleName(); 


    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 


    private GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 

    private GoogleMap mMap; 
    double currentLatitude; 
    double currentLongitude; 
    EditText loc; 
    String loca; 
    String country; 
    TextView myAddress; 
     /*text view for mentioning the destination*/ 





    @ 
    Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 

     mLocationRequest = LocationRequest.create() 
     .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
     .setInterval(10 * 1000) // 10 seconds, in milliseconds 
     .setFastestInterval(1 * 1000); // 1 second, in milliseconds 
    } 

    @ 
    Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
     mGoogleApiClient.connect(); 
    } 

    @ 
    Override 
    protected void onPause() { 
     super.onPause(); 
     super.onPause(); 
     if (mGoogleApiClient.isConnected()) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     mGoogleApiClient.disconnect(); 
     } 
    } 
    //method for current location 
    private void handleNewLocation(Location location) { 
     Log.d(TAG, location.toString()); 

     double currentLatitude = location.getLatitude(); 
     double currentLongitude = location.getLongitude(); 

     LatLng latLng = new LatLng(currentLatitude, currentLongitude); 


     // mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title(coun)); 
     MarkerOptions options = new MarkerOptions() 
     .position(latLng) 
     .title(getCompleteAddressString(currentLatitude, currentLongitude)); 
     mMap.addMarker(options); 
     float zoomLevel = (float) 15.0; 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel)); 
    } 


    //method for placing marker at destination via destination address 
    public void onClick(View view) { 
     loc = (EditText) findViewById(R.id.editText); 
     loca = loc.getText().toString(); 
     List <android.location.Address> addressList = null; 
     if (loca != null || loca.equals("")) { 

     Geocoder gc = new Geocoder(this); 
     try { 
      addressList = gc.getFromLocationName(loca, 1); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     android.location.Address address = addressList.get(0); 
     LatLng ll = new LatLng(address.getLatitude(), address.getLongitude()); 




     mMap.addMarker(new MarkerOptions().position(ll).title("YO")); 
     mMap.animateCamera(CameraUpdateFactory.newLatLng(ll)); 


     } 


    } 





    private void setUpMapIfNeeded() { 
     if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
      .getMap(); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 

      setUpMap(); 
     } 
     } 
    } 

    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
     } 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
     } 
     mMap.setMyLocationEnabled(true); 
    } 



    @ 
    Override 
    public void onConnected(@Nullable Bundle bundle) { 
     Log.i(TAG, "Location services connected."); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
     } 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
     } 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location == null) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } else { 
     handleNewLocation(location); 
     } 

    } 

    @ 
    Override 
    public void onConnectionSuspended(int i) { 
     Log.i(TAG, "Location services suspended. Please reconnect."); 
    } 

    @ 
    Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

     if (connectionResult.hasResolution()) { 
     try { 
      // Start an Activity that tries to resolve the error 
      connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
     } catch (IntentSender.SendIntentException e) { 
      e.printStackTrace(); 
     } 
     } else { 
     Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 
    } 

    @ 
    Override 
    public void onLocationChanged(Location location) { 

    } 

    private String getCompleteAddressString(double latitude, double longitude) { 
     String strAdd = ""; 
     Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
     try { 
     List <android.location.Address> addresses = geocoder 
      .getFromLocation(latitude, longitude, 1); 
     if (addresses != null) { 
      android.location.Address returnedAddress = addresses.get(0); 
      StringBuilder strReturnedAddress = new StringBuilder(""); 

      for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { 
      strReturnedAddress 
       .append(returnedAddress.getAddressLine(i)).append(
       "\n"); 
      } 
      strAdd = strReturnedAddress.toString(); 
     } 
     /* Log.w("My Current loction address", "" + strReturnedAddress.toString()); 
      } else { 
       Log.w("My Current loction address", "No Address returned!"); 
      } */ 
     } catch (Exception e) { 
     e.printStackTrace(); 
     // Log.w("My Current loction address", "Canont get Address!"); 
     } 
     return strAdd; 
    } 
    } 
+1

Bitte erklären Sie die "verschiedenen" Dinge, die Sie ausprobiert haben. –

+0

Und beschreiben Sie das Problem viel spezifischer als "nichts funktioniert". – csmckelvey

+0

Plz überprüfen, ob die Frage jetzt klar ist, thx – Snehil

Antwort

0

sind diese in Ihrer Anwendung build.gradle

dependencies { 
    ... 

    compile 'com.google.maps.android:android-maps-utils:0.4+' 

} 

Diese Bibliothek hat die Klasse PolyUtil. Ich weiß nicht, welche Richtung JSON Parser Sie verwenden, aber ich bin ziemlich sicher, dass es eine Funktion haben wird, die Ihnen die codierte Polylinie String zurückgibt. Und dann verwenden Sie die codierte Zeichenfolge wie unten

public void drawPolyline(){ 
    String polylinePoints = YourParser.getPolylinePoints(); 
    List<LatLng> pointsList = PolyUtil.decode(points); 
    mMap.addPolyline(new PolylineOptions().width(30).color(android.R.color.black).addAll(pointsList)); 

} 
+0

könnten Sie bitte Ihren Code für die Route veröffentlichen? – Snehil

Verwandte Themen