get current Location in Android

Deadly 提交于 2019-12-12 03:09:38

问题


I tried to get the current location in Android. At least, I took this example from this post.

How do I get the current GPS location programmatically in Android?

While this post is five years old, commonsware told me to ask a new question. 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. The dialog appears, requestPosition() is called - and noting happens...

I got feedback from a tester that its not working (nothing happens) on 4.2 - S4, network available.

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

回答1:


It is because : locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, .... ) You have used only Network provider ...

If you want to be sure to get location, register for other providers also.



来源:https://stackoverflow.com/questions/27743973/get-current-location-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!