Sunday, July 8, 2012

[android-developers] Re: [ASK] about get location using GPS

it's work fine. thanks. i just cant use gps on emulator. cos i dont know about emulator control.
when i set the location use emulator control, it works fine. thanks.

On Saturday, May 26, 2012 3:47:12 PM UTC+7, Alfa wrote:
hi, i want to get my location in longitude and latitude from gps, and
i use this code

public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.googlemapview);
                mv = (MapView)findViewById(R.id.mapview);
                LocationManager mlocManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
                //MyLocationListener myLocListener = new MyLocationListener();
                //LocationListener mlocListener = locationListener;


                boolean isGPS = mlocManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // If GPS is not enable then it will be on
            if(!isGPS)
            {
                Intent intent = new
Intent("android.location.GPS_ENABLED_CHANGE");
                intent.putExtra("enabled", true);
                 sendBroadcast(intent);
            }
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
0, 0, locationListener);
}

private final LocationListener locationListener = new
LocationListener() {

        public void onLocationChanged(Location location) {

            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status,
Bundle extras) {
        }
    };

    private void updateWithNewLocation(Location location) {

        String latLongString = "";
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;



        } else {
            latLongString = "No location found";
        }
        Toast.makeText(getBaseContext(),latLongString,
Toast.LENGTH_SHORT).show();

    }
and this is permission i use
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>

this code dont have any error message but it dont have right output.
this is because onLocationChange method not called. i wondder how. can
some one help me?

~Alfa

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment