2016-11-11 6 views
2

Ich arbeitete an Google Maps, brauche ich Google Map Fahrtrichtung zwischen zwei Standorten (mein aktueller Standort und Zielort) in meiner eigenen Anwendung möchte ich keine öffnen Google Maps-Anwendung. also bitte schlage mir vor, wie ich das mache. Bis jetzt habe ich die Integration von Google Maps abgeschlossen, zoom an meinen aktuellen Standort und platziere einen Marker im Ziel lat-long.Google Map Wegbeschreibungen zwischen zwei Standorten in meiner eigenen Anwendung

meine Java-Datei:

public class GoogleMapActivity extends FragmentActivity implements 
    OnMapReadyCallback, GoogleMap.OnMarkerClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

Location mLastLocation; 
GoogleApiClient mGoogleApiClient; 
private GoogleMap mMap; 

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

    SupportMapFragment mapFragment = 
      (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    Log.d("Tag", "in onc control in try"); 
    buildGoogleApiClient(); 
} 

protected void onStart() { 
    mGoogleApiClient.connect(); 
    super.onStart(); 
} 

protected void onStop() { 
    mGoogleApiClient.disconnect(); 
    super.onStop(); 
} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
    mGoogleApiClient.connect(); 
} 
@Override 
public void onConnected(@Nullable Bundle bundle) { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient); 
    if (mLastLocation != null) { 
     Log.d("TAG","lat"+mLastLocation.getLatitude()); 
     Log.d("TAG","lng"+mLastLocation.getLongitude()); 
     ToastHelper.blueToast(getApplicationContext(), "location is " + mLastLocation.getLatitude() + " " + mLastLocation.getLongitude()); 
     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), 12.0f)); 
     LatLng destination = new LatLng(14.880499, 79.988847); 
     mMap.addMarker(new MarkerOptions().position(destination).title("Destination")); 
    } 
    else { 
     Log.d("TAG","mLastLocation is null"); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 
} 

/** 
* Called when the map is ready. 
*/ 
@Override 
public void onMapReady(GoogleMap map) { 
    mMap = map; 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 
} 
/** 
* Called when the user clicks a marker. 
*/ 
@Override 
public boolean onMarkerClick(final Marker marker) { 
    // Retrieve the data from the marker. 
    // Return false to indicate that we have not consumed the event and that we wish 
    // for the default behavior to occur (which is for the camera to move such that the 
    // marker is centered and for the marker's info window to open, if it has one). 
    return false; 
} 

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

} 

}

+0

Warum entscheiden Sie, das zu tun? Es ist machbar, aber machen Sie sich bereit, selbst eine Menge Arbeit zu erledigen. –

+0

danke für den Vorschlag @Martian, ist ihre direkte Google API, um die Richtungen zwischen zwei Standorten zu bekommen? –

Antwort

0

Es ist ein Webservice öffentliche API für Richtungen von Google. Soweit ich weiß, ist es nicht mit Android API gebündelt.

Hier gehen Sie mit dem link die Sie

0

Karo heraus dieses fantastische und leicht zu bedienen Bibliothek erhalten begonnen: https://github.com/jd-alexander/Google-Directions-Android

  • Diese Bibliothek ermöglicht es Ihnen, die Route zwischen zwei Standorten zu berechnen und zeigt es auf einer Karte an.
  • Sie erhalten eine Liste mit Verteilungspunkten, die Sie in Google Map plotten können, um Routen und Wegbeschreibungen anzuzeigen.
0

Ich kenne das Endziel Ihres Projekts nicht. Aber wenn Sie beabsichtigen, eine Wegbeschreibung von Punkt A nach Punkt B zu erhalten, können Sie mit Googles Roads Api die Daten bereitstellen, die Sie benötigen, um die Wegbeschreibungen auf einer Karte zu zeichnen oder was immer Sie möchten.

Es bietet Ihnen die rohen Punkte sogar die Geschwindigkeit in diesem bestimmten Straßensegment. Es kümmert sich um die redundanten Daten, die Sie zum Beispiel dazu bringen könnten, Richtungen durch Gebäude zu zeichnen.

Verwandte Themen