2016-05-20 3 views
0

Ich bin neu in Android und ich möchte meine aktuelle Position Längengrad und Breitengrad zu holen, und ich habe diesen Code verwendet, um meine Breiten - und Längengrad zu holen und ich kann nicht die Ergebnisse in meiner sehen logcat und ich haben auch die google-services.json Datei App/und initialisiert den mGoogleApiClient und mLastLocation und ich bekomme keinen Fehler, warum ich nicht in der Lage bin, die Breite und Länge zu sehen. Bitte helfen Sie mir und danke im Voraus hier ist der vollständige Code meiner App https://github.com/akashmalla07/GoogleMap bitte einen Blick haben, wenn mir jemandNicht in der Lage, aktuellen Standort Breite und Länge in Android

Hier ist der Code von Mapsactivity

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

     private GoogleMap mMap; 
     AppLocationService appLocationService; 
     GoogleApiClient mGoogleApiClient; 
     private Location mLastLocation; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this) 
       .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this) 
       .addApi(LocationServices.API).build(); 

     mLastLocation = LocationServices.FusedLocationApi 
       .getLastLocation(mGoogleApiClient); 
     setUpMapIfNeeded(); 

    } 

    private void setUpMapIfNeeded() { 
     if (mMap == null) { 
      mMap = ((SupportMapFragment) 
      getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      if (mMap != null) { 
       displayLocation(); 
      } 
     } 
    } 

    private void displayLocation() { 

     mLastLocation = LocationServices.FusedLocationApi 
       .getLastLocation(mGoogleApiClient); 

     if (mLastLocation != null) { 
      double latitude = mLastLocation.getLatitude(); 
      double longitude = mLastLocation.getLongitude(); 

      Log.d("result", latitude + ", " + longitude); 

     } else { 

      Log.d("result3","(Couldn't get the location. Make sure location is 
      enabled on the device)"); 
     } 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 

    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 
} 

Und das sind die Importe

import android.location.Location; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 
    import android.util.Log; 

    import com.google.android.gms.common.ConnectionResult; 
    import com.google.android.gms.common.api.GoogleApiClient; 
    import com.google.android.gms.location.LocationListener; 
    import com.google.android.gms.location.LocationServices; 
    import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.SupportMapFragment; 

Aktualisiert Code -------------------------------- -----------

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

private static final String TAG = MapsActivity.class.getSimpleName(); 
private LocationRequest mLocationRequest; 
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000; 
private GoogleMap mMap; 
AppLocationService appLocationService; 
GoogleApiClient mGoogleApiClient; 
private Location mLastLocation; 

private TextView lblLocation; 
private Button btnShowLocation, btnStartLocationUpdates; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    lblLocation = (TextView) findViewById(R.id.lblLocation); 
    btnShowLocation = (Button) findViewById(R.id.btnShowLocation); 
    btnStartLocationUpdates = (Button) 
     findViewById(R.id.btnLocationUpdates); 

    if (checkPlayServices()) { 

     // Building the GoogleApi client 
     buildGoogleApiClient(); 

     } 

    btnShowLocation.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      displayLocation(); 
      Log.d("clicked","yes"); 
     } 
    }); 

} 
protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API).build(); 
} 
private boolean checkPlayServices() { 
    int resultCode = GooglePlayServicesUtil 
      .isGooglePlayServicesAvailable(this); 
    if (resultCode != ConnectionResult.SUCCESS) { 
     if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
      GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
        PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
     } else { 
      Toast.makeText(getApplicationContext(), 
        "This device is not supported.", Toast.LENGTH_LONG) 
        .show(); 
      finish(); 
     } 
     return false; 
    } 
    return true; 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    if (mGoogleApiClient != null) { 
     mGoogleApiClient.connect(); 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    checkPlayServices(); 

} 

@Override 
protected void onStop() { 
    super.onStop(); 
    if (mGoogleApiClient.isConnected()) { 
     mGoogleApiClient.disconnect(); 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 

} 
@Override 
public void onConnectionFailed(ConnectionResult result) { 
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " 
      + result.getErrorCode()); 
} 

@Override 
public void onConnected(Bundle arg0) { 

    displayLocation(); 


} 



private void displayLocation() { 
    Log.d("after","clicked"); 
    mLastLocation = LocationServices.FusedLocationApi 
      .getLastLocation(mGoogleApiClient); 

    if (mLastLocation != null) { 
     double latitude = mLastLocation.getLatitude(); 
     double longitude = mLastLocation.getLongitude(); 

     lblLocation.setText(latitude + ", " + longitude); 
     Log.d("lat",String.valueOf(latitude)); 

    } else { 
     Log.d("lat","No lat"); 

     lblLocation 
       .setText("(Couldn't get the location. Make sure location is enabled on the device)"); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onLocationChanged(Location location) { 

} 

}

+0

Bevor Sie Ihre Frage posten, müssen Sie sich ähnliche Fragen ansehen. Schau mal hier [link] (http://stackoverflow.com/questions/28877835/android-google-maps-v2-current-placement-latitude-longitude-nullpointerexception). Sie werden mehr ähnliche Lösungen finden. –

+0

Können Sie auch zeigen, was ** Importe ** in Ihrer Klasse sind. Oder stellen Sie sicher, dass in Ihrem echten Gerät die Option Standort aktiviert ist. –

+0

@jaydroider in meinem mobilen Gerät Speicherort ist aktiviert und ich habe die Frage bearbeitet, wo Sie die Importe sehen können – user3923278

Antwort

0

Ihr vergessen mGoogleApiClient.connect zu nennen();

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

    private GoogleMap mMap; 
    AppLocationService appLocationService; 
    GoogleApiClient mGoogleApiClient; 
    private Location mLastLocation; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this) 
      .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this) 
      .addApi(LocationServices.API).build(); 

mGoogleApiClient.connect(); 

} 

private void setUpMapIfNeeded() { 
    if (mMap == null) { 
     mMap = ((SupportMapFragment) 
     getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     if (mMap != null) { 
      displayLocation(); 
     } 
    } 
} 

private void displayLocation() { 

    mLastLocation = LocationServices.FusedLocationApi 
      .getLastLocation(mGoogleApiClient); 

    if (mLastLocation != null) { 
     double latitude = mLastLocation.getLatitude(); 
     double longitude = mLastLocation.getLongitude(); 

     Log.d("result", latitude + ", " + longitude); 

    } else { 

     Log.d("result3","(Couldn't get the location. Make sure location is 
     enabled on the device)"); 
    } 
} 

@Override 
public void onConnected(Bundle bundle) { 
      mLastLocation = LocationServices.FusedLocationApi 
      .getLastLocation(mGoogleApiClient); 
    setUpMapIfNeeded(); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onLocationChanged(Location location) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

} 
} 

Ich habe Ihren Code bearbeitet. Sehen Sie, ob das funktioniert.

+0

** D/result3: Der Standort konnte nicht abgerufen werden. Stellen Sie sicher, dass der Standort auf dem Gerät aktiviert ist ** dies ist, dass ich in meinem Logcat – user3923278

+0

bekomme bitte überprüfen, ob GoogleApiClient verbunden ist oder nicht und auch Standorteinstellungen in Ihren mobilen Einstellungen sind aktiviert –

+0

mein Standort in der Einstellung ist aktiviert und können Sie mir sagen Wie überprüfe ich, ob GoogleApiClient verbunden ist oder nicht? – user3923278

0

Es gibt zwei Fehler im Code

Sie sind nicht zu GoogleApiclient verbunden, so wenden mgoogleApiclient.connect nur, nachdem Sie es Builder verwenden bauen.

Rufen Sie Ihre displayLocation() Methode in onConnected() Callback-Methode. Wie dies ist der Erfolg Rückruf von GoogleApiClient, was bedeutet, dass Sie jetzt mit dem Client verbunden sind und Ihren Standort erhalten können.

Außerdem können Sie Standort erhalten, ohne dass GoogleApikey Registrierung, bis Sie nicht Maps oder Places Api in Ihrer Anwendung verwenden.

+0

nachdem ich deiner antwort und einigen anderen tutorials gefolgt bin, habe ich meinen code geändert und bekomme immer noch nicht meine geometrie und dieses mal ist auch mein mobiler standort aktiviert und du kannst den neuen code in meinem editierten post sehen ** aktualisierter code ------ ** – user3923278

+0

Kannst du mir eine andere Möglichkeit geben, den aktuellen Längen- und Breitengrad abzurufen? – user3923278

+0

Versuche, deinen Code zu debuggen, ist GoogleApiClient verbunden? –

Verwandte Themen