gpt4 book ai didi

android - 在 Android 中查找用户的当前位置

转载 作者:行者123 更新时间:2023-11-29 01:18:15 27 4
gpt4 key购买 nike

我是 Android 的新手,我正在尝试开发一个显示用户当前位置的 Android 应用程序。我正在使用 Genymotion。现在我正在使用

mLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleClient); 

获取设备的最后位置。问题是我想获取当前位置,但有了这个我得到 mlocation as not null 这很好。但在 map 中,它始终显示设备的最后位置而不是当前位置。

代码 fragment

@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 天。

最佳答案

发布带有代码的简单解决方案。

在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'

在你的 Activity 中实现这两个接口(interface)

GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener

在 oncreate 中像这样创建 googleapiclient 对象:

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();

并在开始 Activity 时做这个

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();
}
}

完整的 Activity 代码是这样的:

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 日更新--------

获取最新的api检查安卓谷歌帖子: https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html

关于android - 在 Android 中查找用户的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388282/

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