gpt4 book ai didi

java - Android 位置返回 null

转载 作者:行者123 更新时间:2023-12-01 13:45:21 26 4
gpt4 key购买 nike

在尝试确定经度和纬度时遇到一些麻烦,有时有效,有时无效。希望有人愿意看一下,我花了几天时间试图解决这个问题。

在我的 list 权限 ACCESS_FINE_LOCATION 中包含此内容

 private void comenzarLocalizacion()
{

locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mostrarPosicion(loc);

locListener = new LocationListener() {
public void onLocationChanged(Location location) {
mostrarPosicion(location);
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}

};

locManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 30000, 0, locListener);
}

private void mostrarPosicion(Location loc) {
if(loc != null)
{
lblLatitud.setText("Latitud: " + String.valueOf(loc.getLatitude()));
lblLongitud.setText("Longitud: " + String.valueOf(loc.getLongitude()));
lblPrecision.setText("Precision: " + String.valueOf(loc.getAccuracy()));
Log.i("", String.valueOf(loc.getLatitude() + " - " + String.valueOf(loc.getLongitude())));


}
else
{
lblLatitud.setText("Latitud: (cannot be found)");
lblLongitud.setText("Longitud: (cannot be found)");
lblPrecision.setText("Precision: (cannot be found)");
}
}

最佳答案

#Precisely the best way to access location on is through (new) Fused Location Providers API

import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

public class Locations extends IntentService implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {


public Locations() {
super("Locations");
Log.d("Locations", "Location service started... ");
}

private LocationRequest locationRequest;
private LocationClient locationClient;
private Location location;

@Override
protected void onHandleIntent(Intent intent) {
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationClient = new LocationClient(this, this, this);
locationClient.connect();
}

@Override
public void onLocationChanged(Location l) {
// do something on Location change.
}

@Override
public void onConnectionFailed(ConnectionResult arg0) {

}

@Override
public void onConnected(Bundle arg0) {
Log.w("Locations", "Location client connected...");

// get last location
location = locationClient.getLastLocation();
Log.w("Locations", "Latitude : "+location.getLatitude() + "");
Log.w("Locations", "Longitude : "+location.getLongitude() + "");
}

@Override
public void onDestroy() {
if (locationClient.isConnected()) {
locationClient.removeLocationUpdates(this);
}
locationClient.disconnect();
}

@Override
public void onDisconnected() {
}

}

来源 https://developer.android.com/training/location/index.html

关于java - Android 位置返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20407173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com