2017-07-26 1 views
1

Je dois obtenir l'emplacement ou la longitude de la latitude de la classe principale, mais elle renvoie null si j'obtiens l'emplacement ou 0.0 si j'obtiens la latitude et la longitude comme dans ce code ci-dessous. J'ai essayé de minimiser le code et de le rendre clair comme c'est possible.Google maps GPS: la longitude et la latitude retournent zéro

ici est la classe de service où je dois obtenir l'emplacement de la classe principale

public void onLocationChanged(Location location) 
    { 
     Log.e(TAG, "onLocationChanged: " + location); 
     mLastLocation.set(location); 

     if(MainActivity.isMarkerDragged()) { 

      Location markerLocation = new Location(LocationManager.GPS_PROVIDER); 
      markerLocation.setLatitude(MainActivity.getMarkerLat()); 
      markerLocation.setLongitude(MainActivity.getMarkerLon()); 

      Toast.makeText(mContext, MainActivity.getMarkerLon() + " = " + MainActivity.getMarkerLat(), Toast.LENGTH_SHORT).show(); 

      // Current location 
      distance = location.distanceTo(markerLocation); 
      Toast.makeText(mContext, "marker dragged " + distance, Toast.LENGTH_SHORT).show(); 

     } 
    } 

sur la méthode OnLocationChanged je reçois cette longitude et lattitude

endPoint.setTitle(endPoint.getPosition().longitude + ", " + endPoint.getPosition().latitude); 

Cela fonctionne comme prévu, mais quand je obtenir la position sur le service, il renvoie 0.0 (suce)

public static Marker endPoint; 
public static boolean markerDragged = true; 
public static boolean serviceStarted = false; 

private static double markerLat; 
private static double markerLon; 

/** 
* Gets the current location of the device, and positions the map's camera. 
*/ 
private void getDeviceLocation() { 
    /* 
    * Request location permission, so that we can get the location of the 
    * device. The result of the permission request is handled by a callback, 
    * onRequestPermissionsResult. 
    */ 
    if (ContextCompat.checkSelfPermission(this.getApplicationContext(), 
      android.Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 
     mLocationPermissionGranted = true; 
    } else { 
     ActivityCompat.requestPermissions(this, 
       new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
       PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 
    } 
    /* 
    * Get the best and most recent location of the device, which may be null in rare 
    * cases when a location is not available. 
    */ 
    if (mLocationPermissionGranted) { 
     mLastKnownLocation = LocationServices.FusedLocationApi 
       .getLastLocation(mGoogleApiClient); 
    } 

    // Set the map's camera position to the current location of the device. 
    if (mCameraPosition != null) { 
     mMap.moveCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition)); 
    } else if (mLastKnownLocation != null) { 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
       new LatLng(mLastKnownLocation.getLatitude(), 
         mLastKnownLocation.getLongitude()), DEFAULT_ZOOM)); 

     // Adding a marker 
     endPoint = mMap.addMarker(new MarkerOptions() 
       .draggable(true) 
       .position(new LatLng(mLastKnownLocation.getLatitude(), 
         mLastKnownLocation.getLongitude()))); 
     endPoint.setTitle(getString(R.string.drag)); 
    } else { 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM)); 
     mMap.getUiSettings().setMyLocationButtonEnabled(false); 
    } 
} 
@Override 
public void onMarkerDragEnd(Marker marker) { 
    if(!serviceStarted) { 
     startService(new Intent(this, LocationService.class)); 
    } 
} 

@Override 
public void onMarkerDragStart(Marker marker) { 
    setMarkerDragged(true); 

    markerLat = marker.getPosition().latitude; 
    markerLon = marker.getPosition().longitude; 

    endPoint.setTitle(endPoint.getPosition().longitude + ", " + endPoint.getPosition().latitude); 
} 

public static double getMarkerLon(){ 
    return markerLon; 
} 

public static double getMarkerLat(){ 
    return markerLat; 
} 

Répondre

0

Trouvé la solu tion: la classe principale:

 Intent intent = new Intent(this, LocationService.class); 
     intent.putExtra("latitude", endPoint.getPosition().latitude); 
     intent.putExtra("longitude", endPoint.getPosition().longitude); 
     startService(intent); 

et sur le service:

public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    Log.e(TAG, "onStartCommand"); 
    super.onStartCommand(intent, flags, startId); 
    markerLat = (Double) intent.getExtras().get("latitude"); 
    markerLon = (Double) intent.getExtras().get("longitude"); 
    return START_STICKY; 
}