2016-04-24 3 views
1

Ich versuche eine Kartenaktivität zu erstellen, die den aktuellen Standort des Benutzers anzeigt, der auf Straßenebene gezoomt werden kann, aber meine App funktioniert nicht richtig, was sind die Codes, die ich brauche hinzufügen?Karte Aktivität mit Echtzeit-Standortaktualisierung

public class Map_Activity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, 
     GooglePlayServicesClient.OnConnectionFailedListener, 
     com.google.android.gms.location.LocationListener{ 

    private GoogleMap myMap; 
    private LocationClient myLocationClient; 
    private static final LocationRequest REQUEST = LocationRequest.create() 
      .setInterval(5000) 
      .setFastestInterval(16) 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 


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

    } 
    @Override 
    protected void onResume(){ 
     super.onResume(); 
     getMapReference(); 
     wakeUpLocationClient(); 
     myLocationClient.connect(); 
    } 
    @Override 
    public void onPause(){ 
     super.onPause(); 
     if (myLocationClient != null){ 
      myLocationClient.disconnect(); 
     } 
    } 
    private void gotoMyLocation(double lat, double lng) { 
     changeCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(new LatLng(lat, lng)) 
         .zoom(15.5f) 
         .bearing(0) 
         .tilt(25) 
         .build() 
     ), new GoogleMap.CancelableCallback() { 
      @Override 
      public void onFinish() { 
      } 
      @Override 
      public void onCancel() { 
      } 
     }); 
    } 
    private void wakeUpLocationClient(){ 
     if(myLocationClient == null){ 
      myLocationClient = new LocationClient(getApplicationContext(),this,this); 
     } 
    } 
    private void getMapReference(){ 
     if(myMap==null){ 
      myMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
     } 
     if (myMap != null){ 
      myMap.setMyLocationEnabled(true); 
     } 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_map, menu); 
     return true; 
    } 
    @Override 
    public void onConnected(Bundle bundle){ 
     myLocationClient.requestLocationUpdates(REQUEST, this); 

    } 

    @Override 
    public void onDisconnected(){ 

    } 
    @Override 
    public void onLocationChanged(Location location){ 

    } 
    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult){ 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
    private void changeCamera(CameraUpdate update, GoogleMap.CancelableCallback callback) { 

     myMap.moveCamera(update); 
    } 
} 

die App läuft, aber die Karte nicht angezeigt wird, und es wird mir meine aktuelle Position nicht angezeigt :(I jemand hoffe mir

helfen könnte
+0

Sie sind nicht die Karte bekommen, weil Sie nicht "die Karte bekommen". Sie müssen 'getMapAsync()' aufrufen und den 'onMapReadyCallBack() 'Callback [gemäß der offiziellen Anleitung] (https://developers.google.com/maps/documentation/android-api/intro) implementieren. Vielleicht wird jemand eine richtige Antwort schreiben, die das genauer erklärt. –

Antwort

0

Hier sind Schritt für Schritt für die Verwendung von Googlemaps:

A.configured Ihr Projekt mit Googlemaps (Ihre Karte, wenn nicht richtig konfiguriert möglicherweise nicht angezeigt) zu arbeiten:

  1. in „build.graddle“ add Abhängigkeiten mit der richtigen Version zB 6.5.87 oder 8.1.0:
  2. in "Android.Manifest" Hinzufügen von Berechtigungen, GL und API Key (stellen Sie sicher, dass Ihre apikey setzen):

    < uses-permission android:name="android.permission.WAKE_LOCK" /> < uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> < uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> < uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> < uses-permission android:name="android.permission.INTERNET" /> < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> < uses-feature android:glEsVersion="0x00020000" android:required="true"/> <!-- Google Maps Fragment API Key Data --> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR API KEY" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

B. Code:

  1. Verwenden Sie OnMapReady:

    • add "OnMapReadyCallback", um Ihre Aktivitätsklasse: public class Map_Activity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener, OnMapReadyCallback {

    • Überschreibung onMapReady Lage zu ermöglichen und haben Bezug auf GoogleMap:

      @Override public void onMapReady(GoogleMap map) { map.setMyLocationEnabled(true); googleMap = map; }

  2. in Buff OnCrate getMapAsync nennen:

    FragmentManager fm = getSupportFragmentManager(); SupportMapFragment mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map); mapFragment.getMapAsync(this);

C. Entfernen/Ersetzen veralteter Code:

-LocationClient 
-replace "GooglePlayServicesClient" with GoogleApiClient