2016-10-15 5 views
0

Ich versuche, entlang einer einfachen Google Maps Android Tutorial folgen, aber ich habe ein Problem mit meinem GoogleMap-Objekt, es scheint, als ob es den Wert von null, wenn die onLocationChanged-Methode ist gefeuert, habe ich versucht, nach dem Null-Objekt innerhalb dieser Methode zu suchen, aber dadurch wird die Markierung nicht gesetzt.GoogleMap ist auf null onLocationChanged eingestellt

Ist dies die Methode, onLocationChanged zu verwenden? Weil Kommentare, die ich von Google gesehen habe, anzeigen, dass die onMapReady-Funktion zum Hinzufügen von Markern und Listenern verwendet wird. Muss ich meinen LocationListener anders anschließen?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 

    public static final String TAG = MapsActivity.class.getSimpleName(); 

    private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

    private GoogleMap mMap; 
    private GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 

    @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); 

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

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

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

     // no prior location 
     if(location == null) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } else { 
      handleNewLocation(location); 
     } 

     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 

     Log.i(TAG, "Latitude: " + latitude + " Longitude" + longitude); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.i(TAG, "LocationServices has been 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 
    protected void onResume() { 
     super.onResume(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if(mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    private void handleNewLocation(Location location) { 

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

     LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

     MarkerOptions markerOptions = new MarkerOptions().position(latLng).title("I am here!"); 

     mMap.addMarker(markerOptions); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     handleNewLocation(location); 
    } 

}

+0

Arbeitscode hier: http://stackoverflow.com/questions/34582370/how-can-i-show-current-location-on-a-google-map-on-android-marshmallow –

Antwort

0

Vom documentation:

A GoogleMap muss mit getMapAsync (OnMapReadyCallback) erworben werden. Diese Klasse initialisiert automatisch das Kartensystem und die Ansicht.

Sie vermissen den Anruf getMapAsync(). Sie sollten es zu onCreate() hinzufügen.

Und man könnte dann entweder einen boolean true mapReady = true; in onMapReady() gesetzt und überprüfen
if (mapReady) { // Do stuff }
oder nur immer überprüfen
if (mMap != null) { // Do stuff }
wenn es zu benutzen.

Sonst sieht der Code auf den ersten Blick gut aus, aber ich habe ihn in meinem Kopf nicht wirklich debugged.

1

Sie müssen innerhalb der onMapReady() -Lokalisierer Location Listener hinzufügen, so dass Sie erst nach Google Map bereit ist, den Rückruf für die Standortänderung zu erhalten.

Verwandte Themen