我有这段代码来请求 Android 中某人的位置,有人可以向我解释一下为什么它不起作用,在位置请求之后,它设置两个 EditText 字段作为纬度和经度值
package com.datapost.location;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class Main extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
onStart();
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
}
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
EditText mLatitudeText = (EditText) findViewById(R.id.lat);
EditText mLongitudeText = (EditText) findViewById(R.id.lon);
String s = String.valueOf(mLastLocation.getLatitude());
String f = String.valueOf(mLastLocation.getLongitude());
mLatitudeText.setText(s);
mLongitudeText.setText(f);
}
}
@Override
public void onConnectionSuspended(int i) {
return;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
connectionResult.getErrorCode();
return;
}
}
我不知道为什么它不起作用,尝试了一切似乎是一项基本工作,但它只是不想工作,我是否必须有 API key 或其他东西,我是否需要一个只是为了从他们的手机获取一个人的位置。 任何帮助表示感谢
我还可以发布我收到的错误消息
错误
06-13 15:54:56.311 11320-11320/com.datapost.location W/System: ClassLoader referenced unknown path: /data/app/com.datapost.location-1/lib/x86_64
06-13 15:54:56.339 11320-11320/com.datapost.location I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
06-13 15:54:56.906 11320-11320/com.datapost.location W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-13 15:54:57.032 11320-11366/com.datapost.location D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 06-13 15:54:57.041 11320:11320 D/ ]
HostConnection::get() New Host Connection established 0x7f6d81a1a480, tid 11320
[ 06-13 15:54:57.093 11320:11366 D/ ]
HostConnection::get() New Host Connection established 0x7f6d85b50840, tid 11366
06-13 15:54:57.109 11320-11366/com.datapost.location I/OpenGLRenderer: Initialized EGL, version 1.4
我将 If 语句的代码更改为这样
@Override
public void onConnected(Bundle bundle) {
if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
EditText mLatitudeText = (EditText) findViewById(R.id.lat);
EditText mLongitudeText = (EditText) findViewById(R.id.lon);
String s = String.valueOf(mLastLocation.getLatitude());
String f = String.valueOf(mLastLocation.getLongitude());
mLatitudeText.setText(s);
mLongitudeText.setText(f);
}
}
}
最佳答案
您需要从 Google 控制台启用 geoLocation API 并创建一个 key 并在您的应用程序菜单中使用该 key ,如下所示:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
还检查 android M+ 的运行时权限
关于java - 在android中请求位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37791412/