简体   繁体   English

在Android中获取当前位置

[英]get current Location in Android

I tried to get the current location in Android. 我试图获取Android中的当前位置。 At least, I took this example from this post. 至少,我从这篇文章中得到了这个例子。

How do I get the current GPS location programmatically in Android? 如何在Android中以编程方式获取当前GPS位置?

While this post is five years old, commonsware told me to ask a new question. 虽然这篇文章已有5年历史了,但commonsware告诉我提出一个新问题。 Here is my code Snippet. 这是我的代码段。

@Override
public void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    Log.i(TAG, "onCreate");
    setHasOptionsMenu(true);
    mActivity = (CustomerTabs) this.getActivity();
    mActivity.getActionBar().hide();

    if (checkGooglePlayServicesAvailable()){
        enableTabs(false);
        showDialog(true, null);
        requestPosition();
    }


boolean checkGooglePlayServicesAvailable() {
    final int connectionStatusCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(getActivity());
    Log.i(TAG,
            "checkGooglePlayServicesAvailable, connectionStatusCode="
                    + connectionStatusCode);
    if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
        //showDialog(false,null);
        showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
        return false;
    }
    return true;
}

void showGooglePlayServicesAvailabilityErrorDialog(
        final int connectionStatusCode) {
    getActivity().runOnUiThread(new Runnable() {
        public void run() {
            try {
                final Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
                        connectionStatusCode, getActivity(),
                        101);
                if (dialog == null) {
                    Log.e(TAG,
                            "couldn't get GooglePlayServicesUtil.getErrorDialog");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });
}

    void requestPosition(){
    Log.i(TAG,"requestPosition");

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    try {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                60000, 1000,
                onLocationChange);
    }catch (Exception e){
        e.printStackTrace();
        showDialog(false, null);
        enableTabs(true);
        displayMessage(getString(R.string.position_fail));
    }
}

LocationListener onLocationChange=new LocationListener() {
    @Override
    public void onLocationChanged(android.location.Location location) {
        Log.i(TAG, "onLocationChange " + location.getLatitude() + "/" +    location.getLongitude());
        TemporaryPersister.getInstance().setCurrentPosition(new LatLng(location.getLatitude(),location.getLongitude()));
        requestLocations();
    }

permissions at manifest: 清单中的权限:

<permission
    android:name="versatec.organightandroid.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="versatec.organightandroid.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES "/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

EDIT: 编辑:

This solution is working on 4.1 (S3 mini) with and without WiFi but not working on another device (S3: without SIM - problem?) in WiFi. 此解决方案可在带或不带WiFi的4.1(S3 mini)上使用,但不能在带WiFi的另一设备(S3:不带SIM-问题?)上使用。 The dialog appears, requestPosition() is called - and noting happens... 出现对话框,调用requestPosition()-并注意发生...

I got feedback from a tester that its not working (nothing happens) on 4.2 - S4, network available. 我从测试人员那里得到反馈,认为它在4.2-S4(网络可用)上不起作用(什么也没有发生)。

Log: 日志:

I/Google Maps Android API﹕ Google Play services package version: 6599036
I/Map﹕ onCreate
I/Map﹕ checkGooglePlayServicesAvailable, connectionStatusCode=0
I/Map﹕ showDialog true
I/Map﹕ requestPosition
I/Map﹕ onCreateView
I/Map﹕ onActivityCreated

It is because : locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, .... ) You have used only Network provider ... 这是因为:locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,....)您仅使用了网络提供程序...

If you want to be sure to get location, register for other providers also. 如果您想确保获得位置,请同时注册其他提供商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM