Android – unable to get location using Google’s converged location API
•
Android
I'm trying to use Google's fusion location API to get location. To do this, I created two classes. One is mainactivity, the second is fusedlocationservice, where mainactivity is the main class. But my longitude and latitude are 0.0. So please help me
This is my mainactivity Code:
public class MainActivity extends AppCompatActivity {
TextView tvLocation;
FusedLocationService fusedLocationService;
double latitude;
double longitude;
String locationResult = "";
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLocation = (TextView) findViewById(R.id.tvLocation);
fusedLocationService = new FusedLocationService(this);
location = fusedLocationService.getLocation();
if (null != location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
locationResult = "Latitude: " + latitude + "\n" +
"Longitude: " + longitude + "\n";
} else {
Timber.e("-error-%s", "Location Not Available!");
locationResult = "Location Not Available!";
}
Log.e("Lati ", String.valueOf(latitude));
Log.e("longi ", String.valueOf(longitude));
tvLocation.setText(locationResult);
}
}
This is my fusedlocationservice class:
public class FusedLocationService implements LocationListener, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final long INTERVAL = 1000 * 30; //30sec
private static final long FASTEST_INTERVAL = 1000 * 5; // 5sec
Activity mActivity;
public LocationRequest locationRequest;
public GoogleApiClient googleApiClient;
public Location location;
public FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
public FusedLocationService(Activity activity) {
Timber.plant(new Timber.DebugTree());
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);
mActivity = activity;
googleApiClient = new GoogleApiClient.Builder(mActivity)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
if (googleApiClient != null) {
googleApiClient.connect();
}
}
@Override
public void onConnected(Bundle connectionHint) {
Log.e("-onConnected-", "connected Now");
location = fusedLocationProviderApi.getLastLocation(googleApiClient);
fusedLocationProviderApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
}
public Location getLocation() {
return location;
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
I have included permissions in the list and Lib in the gradle folder. So please help. Thank you first
resolvent:
Missing reference to new location:
@Override
public void onLocationChanged(Location location) {
this.location = location;
}
Location updates are also asynchronous. Therefore, you need something like an active callback
Edit:
Also check whether the Google play service is available. You can create such a method:
public boolean checkPlayServices() {
return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS;
}
View Google documentation
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码