在android中请求位置

时间:2016-06-13 13:44:40

标签: 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密钥或东西,我需要一个只是为了从一个人的位置他们的电话。 任何帮助赞赏这个

我也可以发布我收到的错误消息

错误

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

}

1 个答案:

答案 0 :(得分:0)

您需要从谷歌控制台启用geoLocation API并创建一个密钥,并在您的应用程序清单中使用该密钥,如下所示:

myDialog

还要检查android M +

的运行时权限