在Android中查找用户的当前位置 [英] Finding current location of the user in Android
问题描述
我是Android的新手,我正在尝试开发一个显示用户当前位置的Android应用.我正在使用Genymotion.现在我正在使用
I am new to Android and I am trying to develop an Android app which shows current location of the user. I am using Genymotion. Now I am using
mLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleClient);
获取设备的最后位置.事情是我想要获取当前位置,但是有了这个,我得到mlocation
的值不是非null就可以了.但是在地图中,它始终显示设备的最后位置,而不是当前位置.
to get the last location of the device. The thing is I wanted to get the current location but with this I get mlocation
as not null that's fine. But in the map it shows last location of the device always not the current location.
代码段
@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()
.
and in onstart()
I called googleapi.connect()
and I used setUpMapIfNeeded()
appropriately.
请告诉我这段代码有什么问题.我正在尝试3天.
Please tell me whats the problem in this code. I am trying this for 3 days.
推荐答案
发布带有代码的简单解决方案.
posting simple solution with code.
在AndroidManifest.xml文件中添加权限
add permissions inside AndroidManifest.xml file
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
如果您的应用程序兼容棉花糖,请检查运行时权限.
if you app is marshmallow compatible then check run time permission.
在gradle文件中添加依赖项:
add dependency inside gradle file:
compile 'com.google.android.gms:play-services:9.2.0'
在您的活动中实现这两个界面
implements this two interface in you activity
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
和
在oncreate中创建像这样的googleapiclient对象:
create googleapiclient object like this in oncreate:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
并在开始活动时进行
mGoogleApiClient.connect();
在这里,我们继续执行此覆盖方法上的location的结果回调.
here we go on result callback of location on this override method.
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.
p.s. googleapiclient is not working in emulator without play services, so you have to test it on real devices. please make sure gps is enable on that device.
如果要使用传统方法获取位置,请尝试使用本教程. http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
if you want to use traditional method for get location then try this tutorial. http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
------更新6月15日---------
获取最新的API 检查android Google帖子: https://android-developers. googleblog.com/2017/06/reduce-friction-with-new-location-apis.html
for latest apis check android google post: https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html
这篇关于在Android中查找用户的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!