我正在尝试使用Google的融合位置API获取我的位置。为此,我创建了两个类。一个是MainActivity,第二个是FusedLocationService,其中MainActivity是主类。但我的经度和纬度为0.0。所以请帮助我。
这是我的MainActivity代码: -
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);
}
}
这是我的FusedLocationService类: -
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) {
}
}
我已在gradle文件夹中的清单和lib中包含权限。 所以请帮忙。提前谢谢
答案 0 :(得分:2)
缺少对新位置的引用:
@Override
public void onLocationChanged(Location location) {
this.location = location;
}
位置更新异步。所以你需要像活动回调这样的东西。
编辑:
还要检查Google Play服务是否可用。您可以创建一个这样的方法:
public boolean checkPlayServices() {
return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS;
}
答案 1 :(得分:1)
简短说明
您获得全球纬度和经度变量的默认值
double latitude;
double longitude;
长解释
实例化FusedLocationService类
时fusedLocationService = new FusedLocationService(this);
它构建谷歌api客户端然后尝试连接
public FusedLocationService(Activity 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);
}
连接google api,接收位置是异步操作。
这意味着,你必须倾向于他们的回调
但是在初始化FusedLocationService类之后,您正在使用纬度经度变量。所以这就是你将0.0作为纬度和经度的原因。
Log.e("Lati ", String.valueOf(latitude));
Log.e("longi ", String.valueOf(longitude));
tvLocation.setText(locationResult);
编辑 - 完成工作
首先,您可以删除或移动这些代码。您正在处理异步操作。
Log.e("Lati ", String.valueOf(latitude));
Log.e("longi ", String.valueOf(longitude));
tvLocation.setText(locationResult);
当google api连接时,检查以前收到的位置并更新你的ui(如果有的话)
@Override
public void onConnected(Bundle connectionHint) {
...
Location location = fusedLocationProviderApi.getLastLocation(googleApiClient);
// Provider could return null, because it tries to get location if any
if(location != null){
// UPDATE YOUR UI
}
fusedLocationProviderApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
收到新位置后,请更新ui
@Override
public void onLocationChanged(Location location) {
// You have received fresh location
// UPDATE YOUR UI
}
同时检查此link,您将更好地理解我提到的方面。
答案 2 :(得分:0)
我创建了一个名为LocationActivity的类
public class LocationActivitity extends AppCompatActivity
implements LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
final String TAG = "GPS";
private long UPDATE_INTERVAL = 2 * 1000; /* 10 secs */
private long FASTEST_INTERVAL = 2000; /* 2 sec */
static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
GoogleApiClient gac;
LocationRequest locationRequest;
public String tvLatitude, tvLongitude, tvTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isGooglePlayServicesAvailable();
if(!isLocationEnabled())
showAlert();
locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
gac = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onStart() {
gac.connect();
super.onStart();
}
@Override
protected void onStop() {
gac.disconnect();
super.onStop();
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
updateUI(location);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
return;
}
Log.d(TAG, "onConnected");
Location ll = LocationServices.FusedLocationApi.getLastLocation(gac);
Log.d(TAG, "LastLocation: " + (ll == null ? "NO LastLocation" : ll.toString()));
LocationServices.FusedLocationApi.requestLocationUpdates(gac, locationRequest, this);
}
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission was granted!", Toast.LENGTH_LONG).show();
try{
LocationServices.FusedLocationApi.requestLocationUpdates(
gac, locationRequest, this);
} catch (SecurityException e) {
Toast.makeText(this, "SecurityException:\n" + e.toString(), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Permission denied!", Toast.LENGTH_LONG).show();
}
return;
}
}
}
@Override
public void onConnectionSuspended(int i) {}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(this, "onConnectionFailed: \n" + connectionResult.toString(),
Toast.LENGTH_LONG).show();
Log.d("DDD", connectionResult.toString());
}
private void updateUI(Location loc) {
Log.d(TAG, "updateUI");
tvLatitude=loc.getLatitude()+"";
tvLongitude=loc.getLongitude()+"";
//tvTime.setText(DateFormat.getTimeInstance().format(loc.getTime()));
}
private boolean isLocationEnabled() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
private boolean isGooglePlayServicesAvailable() {
final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
.show();
} else {
Log.d(TAG, "This device is not supported.");
finish();
}
return false;
}
Log.d(TAG, "This device is supported.");
return true;
}
private void showAlert() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enable Location")
.setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
"use this app")
.setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
finish();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
}
});
dialog.show();
}
}
以下是获取位置的方法updateUI(位置loc)。