【发布时间】:2017-02-09 02:26:02
【问题描述】:
显然,我正在尝试编写一个 android 应用程序,它使用用户的位置在地图活动上放置一个标记,或者在这种情况下打印纬度和经度。但是,我找不到适合我的教程或以前的问题。我正在寻找使用 gps 以 Location 对象或纬度和经度的形式获取用户当前位置的绝对最简单的方法。我目前的代码如下,直接来自this tutorial:
public class MainActivity extends AppCompatActivity
implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private TextView lat, lng;
private GoogleApiClient mGoogleApiClient;
private Location mCurrentLocation;
private boolean mRequestingLocationUpdates;
private LocationRequest mLocationRequest;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lat = (TextView) findViewById(R.id.lat);
lng = (TextView) findViewById(R.id.lng);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected() && !mRequestingLocationUpdates) {
startLocationUpdates();
}
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
@Override
public void onConnected(Bundle bundle) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mCurrentLocation != null) {
lat.setText(String.valueOf("Lat: " + mCurrentLocation.getLatitude()));
lng.setText(String.valueOf("Lng: " + mCurrentLocation.getLongitude()));
}
else
Log.i("Problem", "mCurrentLocation is null");
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
updateUI();
}
private void updateUI() {
lat.setText(String.valueOf("Lat: " + mCurrentLocation.getLatitude()));
lng.setText(String.valueOf("Lng: " + mCurrentLocation.getLongitude()));
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
我的 AndroidManifest.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.root.locationtest">
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="LocationTest"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我似乎总是收到日志消息“mCurrentLocation 为空”,因此应用程序中没有任何变化。 mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 行上还有一条警告,上面写着“呼叫需要权限,可能会被用户拒绝”。任何帮助表示赞赏,在此先感谢您。
编辑: 我用 if 语句将所有行都用“调用需要用户可能拒绝的权限”括起来:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED)
解决了这个警告。但是,onConnect() 方法中的权限被拒绝,并且 mCurrentLocation 仍然为空。
【问题讨论】:
-
添加您的 AndroidManifest.xml 文件
-
请尝试在此答案中按照我的教程进行操作。这将帮助您获取当前位置、纬度、经度,它还会实时更新您当前的纬度和经度。 stackoverflow.com/a/42131361/4201474
标签: android google-maps gps location