android - 如何获取位置?

标签 android location android-permissions

我正在开发我的第一个 Android 应用程序,它应该获取 android 设备的纬度和经度,并通过 Web 服务将其发送到模板文档。

这是我的 .java 类的代码:

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
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 GetLocation extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private GoogleApiClient mGoogleApiClient;
    EditText textLat;
    EditText textLong;
    EditText lat;
    EditText lon;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.get_location);

        textLat = (EditText) findViewById(R.id.latitude);
        textLong = (EditText) findViewById(R.id.longitude);

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

    private boolean isGPSEnabled() {
        LocationManager cm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        return cm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    @Override
    public void onConnected(Bundle bundle) {
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            lat.setText(String.valueOf(mLastLocation.getLatitude()));
            lon.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    public void onButtonClick(View v){
        if(v.getId() == R.id.getGpsLocation){
            if(!isGPSEnabled()){
                new AlertDialog.Builder(this)
                        .setMessage("Please activate your GPS Location!")
                        .setCancelable(false)
                        .setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(i);
                            }
                        })
                        .setNegativeButton("Cancel", null)
                        .show();
            } else {
                textLat.setText(String.valueOf(lat));
                textLong.setText(String.valueOf(lon));
            }

        }
    }


}

我没有收到任何错误,但是当我点击应该获取坐标的按钮时,我在两个 View 中都得到了“空”文本。

我还包括互联网权限,访问精细和粗略的位置。

提前致谢!

最佳答案

您应该使用 GooglePlayServices

compile 'com.google.android.gms:play-services-location:7.0.0'

获取位置
if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
}
if (mGoogleApiClient != null) {
    mGoogleApiClient.connect();
}

连接后
public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {
    ...
    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }
}

关于android - 如何获取位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59284230/

相关文章:

android - 在我使用 Fused Location Client 的 android 应用程序中,为什么我在使用此代码时会得到陈旧的 GPS

Android静音/取消静音手机

android - 设置权限更改的广播接收器

android - 有没有办法在应用程序中以编程方式捕获清除数据操作?

javascript - 我的 html 网站未在移动设备上显示

java - Android:添加 SEND_SMS 权限导致 gradle build 失败

android,无需遍历所有位置即可找到最近的位置

windows - Windows 中供应商的 OpenGL 驱动程序在哪里?

android - 从某个元素访问 ListView 的其他元素的 View

android - 如何修复 Android Manifest 文件中的错误 "Unsupported type ' uses-permission'"