我是Android的新用户,我正在尝试开发一个显示用户当前位置的Android应用。我正在使用Genymotion。现在我正在使用
mLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleClient);
获取设备的最后位置。问题是我想要获取当前位置但是我得到mlocation
因为没有空,那很好。但在地图中,它显示设备的最后位置始终不是当前位置。
代码段
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "inside onconnected Location services connected");
Location mLocation=null;
mLocationRequest= new LocationRequest();
mLocationRequest.setInterval(10 * 1000) ;
mLocationRequest.setFastestInterval(1 * 1000);mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
}
mLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleClient);
if(mLocation==null)
{
Log.d("***Inside mlocation***", "***mlocation is null here");
}
if (mLocation != null) {
onLocationChanged(mLocation);
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude,currentLongitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("Im here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
并在onstart()
我致电googleapi.connect()
,我恰当地使用了setUpMapIfNeeded()
。
请告诉我这个代码中的问题是什么。我这样做了3天。
答案 0 :(得分:1)
使用代码发布简单的解决方案。
在AndroidManifest.xml文件中添加权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
如果您的应用与marshmallow兼容,请检查运行时权限。
在gradle文件中添加依赖项:
compile 'com.google.android.gms:play-services:9.2.0'
在您的活动中实现这两个界面
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
和
在oncreate中创建像这样的googleapiclient对象:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
并在开始活动中进行此操作
mGoogleApiClient.connect();
这里我们在这个覆盖方法上进行位置的结果回调。
public void onConnected(@Nullable Bundle bundle) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
Log.e(TAG, "onConnected: " + String.valueOf(mLastLocation.getLatitude()) + ":" + String.valueOf(mLastLocation.getLongitude()));
} else {
Toast.makeText(getApplicationContext(), "Your Location Not Found", Toast.LENGTH_LONG).show();
}
}
完整的活动代码是这样的:
public class LocationActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = LocationActivity.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
@Override
protected void onStart() {
// connect googleapiclient
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
// disconnect googleapiclient
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
// here we go you can see current lat long.
Log.e(TAG, "onConnected: " + String.valueOf(mLastLocation.getLatitude()) + ":" + String.valueOf(mLastLocation.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}}
p.s。 googleapiclient在没有播放服务的情况下无法在模拟器中运行,因此您必须在真实设备上进行测试。 请确保在该设备上启用gps。
如果您想使用传统方法获取位置,请尝试本教程。 http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
------ 6月15日更新---------
最新的apis 检查android谷歌帖子: https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html
答案 1 :(得分:0)
对于地图服务,它需要Google Play服务。确保您的Genymotion安装了PlayService,如果没有,请从Here获取帮助以在Genymotion中安装Google Play服务。 快乐的编码.. !!!
答案 2 :(得分:0)
LocationListener locationListener;
LocationManager locationManager;
double latitude;
double longitude;
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
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.
;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0, locationListener);
Log.e("location", longitude + " " + latitude);