java - 在android中请求位置

标签 java android

我有这段代码来请求 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/

相关文章:

java - 从names.nsf目录中检索拒绝访问组成员

java - 无法从不同类别启动动画到另一个类别

java - 在 tomcat 服务器上的 Web 应用程序中使用 SVNKit 时 JRE 崩溃

android - 使用 intent-filter 通过 Adob​​e AIR 应用程序打开文件类型

android - Android平台的FFmpeg

android - 根据当前位置删除数据库行

java - 具有通用返回类型的 vararg 方法中的 @SafeVarargs

java - Jersey 序列化/反序列化问题 : abstract types can only be instantiated with additional type information

android - 在 ConstraintLayout 中,当我将 View 的布局方向设置为 "rtl"时,出现了一些问题。如何避免这种情况?

android - 通过普通 HTTP 请求 Deezer 轨道时没有流值