我的代码:
mGoogleapiclient = new Googleapiclient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addAPI(LocationServices.API) .build();locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);@OverrIDepublic voID onConnected(Bundle bundle) { mLocation = LocationServices.FusedLocationAPI.getLastLocation(mGoogleapiclient); double lat = mLocation.getLatitude(); double lon = mLocation.getLongitude();}
我在lat和lon变量中没有得到任何东西.
我该如何解决?
解决方法 它没有必要LocationServices.FusedLocationAPI.getLastLocation(mGoogleapiclient);将始终返回最后一个对象.如果它找不到最后一个对象,它将返回null并且您将无法获得位置.在这种情况下,建议使用以下方式请求定位:LocationServices.FusedLocationAPI.requestLocationUpdates(mGoogleapiclient,mLocationRequest,this);
您还需要实现一些接口和覆盖方法,以通过Google Api获取位置.
按照以下步骤获取位置:
首先,将它放在gradle文件中
compile 'com.Google.androID.gms:play-services:8.4.0'
然后实现必要的接口
public class MainActivity extends BaseActivitiy implements Googleapiclient.ConnectionCallbacks,Googleapiclient.OnConnectionFailedListener,com.Google.androID.gms.location.LocationListener
声明实例
private Googleapiclient mGoogleapiclient; private Location mLocation; private LocationManager locationManager; private LocationRequest mLocationRequest;
把它放在onCreate()
mGoogleapiclient = new Googleapiclient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addAPI(LocationServices.API) .build(); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
最后,重写必要的方法
@OverrIDe public voID onConnected(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) { // Todo: ConsIDer calling // ActivityCompat#requestPermissions // here to request the missing permissions,and then overrIDing // public voID onRequestPermissionsResult(int requestCode,String[] permissions,// int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } mLocation = LocationServices.FusedLocationAPI.getLastLocation(mGoogleapiclient); if(mLocation == null){ startLocationUpdates(); } if (mLocation != null) { double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); } else { // Toast.makeText(this,"Location not Detected",Toast.LENGTH_SHORT).show(); } } protected voID startLocationUpdates() { // Create the location request mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); // Request location updates if (ActivityCompat.checkSelfPermission(this,// int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } LocationServices.FusedLocationAPI.requestLocationUpdates(mGoogleapiclient,this); Log.d("reque","--->>>>"); } @OverrIDe public voID onConnectionSuspended(int i) { Log.i(TAG,"Connection Suspended"); mGoogleapiclient.connect(); } @OverrIDe public voID onConnectionFailed(ConnectionResult connectionResult) { Log.i(TAG,"Connection Failed. Error: " + connectionResult.getErrorCode()); } @OverrIDe public voID onStart() { super.onStart(); mGoogleapiclient.connect(); } @OverrIDe public voID onStop() { super.onStop(); if (mGoogleapiclient.isConnected()) { mGoogleapiclient.disconnect(); } } @OverrIDe public voID onLocationChanged(Location location) { }总结
以上是内存溢出为你收集整理的如何在Android App中使用Google API获取当前GPS位置(经度 – 纬度)?全部内容,希望文章能够帮你解决如何在Android App中使用Google API获取当前GPS位置(经度 – 纬度)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)