2016-05-24 13 views
0

Ich versuche, die Google Maps API zu implementieren. Ich möchte meine eigene Position auf der Karte sehen und ich möchte, dass es sich ändert, wenn ich mich signifikant bewege. Das Problem ist, dass es keine Fehler gibt, aber es zeigt keine Markierung meiner Position auf der Karte an. Das ist mein onCreate() Methode in der .java-DateiGoogle Maps Android API und aktueller Standort

private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
public static final String TAG = MapPane.class.getSimpleName(); 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map_pane); 
    // 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); 

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

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

Und das ist die Methode, die ich denke, ich bin hier fehlt etwas

public void onConnected(@Nullable Bundle bundle) { 
    Log.i(TAG, "Location services connected."); 

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null) { 
     Log.i(TAG, "loc exists"); 
     if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
       || checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     }//if 
    }//if 
    else { 
     handleNewLocation(location); 
    }//else 
}//onConnected 

, weil ich nie if-Anweisung in die zweite bekommen. Ich denke, ich brauche zuerst die Erlaubnis des Benutzers? Das manuelle Einschalten des GPS auf meinem Gerät funktioniert auch nicht. Und das Verfahren unten habe ich für auf der Karte die Markierung anzeigt

private void handleNewLocation(Location location) { 
    Log.d(TAG, location.toString()); 
    double currentLatitude = location.getLatitude(); 
    double currentLongitude = location.getLongitude(); 
    LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

    MarkerOptions options = new MarkerOptions() 
      .position(latLng) 
      .title("I am here!"); 
    mMap.addMarker(options); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

}//handleNewLocation 

ich mehr Methoden verwendet, aber ich denke, es gibt keinen Fehler drin. Im AndroidManifest.xml I die

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
+0

Wie hoch ist der Wert des Standorts? –

+0

Es ist null, weil ich das "Log.i (TAG," loc existiert ") sehen kann;" in meinem Android Monitor – Qas

+0

hast du die Methoden onStart() und onStop() wie dieses Tutorial https://developer.android.com/training/location/retrieve-current.html? –

Antwort

0

Sie Location api-Client anstelle von google

   LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
    final Geocoder gc = new Geocoder(this, Locale.getDefault()); 

    LocationListener locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      //here you can get current position location.getLatitude(),location.getLongitude(); 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
0

Es scheint, Ihr Code bereitstellt, keine Position zu der handlelocation() Methode verwenden können.

private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
public static final String TAG = MapPane.class.getSimpleName(); 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
LocationListener li; 

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

li=new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      handleNewLocation(location); 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 


    getHandledCurrentLocation(); 
}//onCr 

private void getHandledCurrentLocation() { 
    String locationProvider = LocationManager.NETWORK_PROVIDER; 

    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; 
    } 
    Log.e("LOCATION_DEBUG","LOCATION REQUESTED--- "); 
    LCM.requestLocationUpdates(locationProvider, 10 * 1000, 0,li); 

} 
private void handleNewLocation(Location location) { 
Log.d(TAG, location.toString()); 
double currentLatitude = location.getLatitude(); 
double currentLongitude = location.getLongitude(); 
LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

MarkerOptions options = new MarkerOptions() 
     .position(latLng) 
     .title("I am here!"); 
mMap.addMarker(options); 
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

}//handleNewLocation 

dieser Code sollte funktionieren.

+0

Benachrichtigen Sie mich, wenn Fehler angezeigt werden .. –

0

Ich kann einige Probleme mit Ihrem Code sehen. Ich glaube nicht, dass Sie die Standortaktualisierungen von FusedLocationApi verarbeiten. Ich glaube, dass Sie LocationListener implementieren, obwohl dieser Teil des Codes in Ihrer Frage nicht bereitgestellt wird. In diesem Fall implementieren Sie in Ihrer Aktivität eine Methode 'onLocationChanged(Location location)'. Mit dieser Methode sollten Sie versuchen, den neuen Standort auf der Karte festzulegen. Ihre handleNewLocation Methode wird nicht automatisch von FusedLocationApi aufgerufen

Sie können den vollständigen Code für Ihre Aktivität bereitstellen, dann können wir Ihnen besser helfen.

Verwandte Themen