Have you enable your GPS first?
2012-5-28 上午7:36 於 "Alfa" <672008...@student.uksw.edu> 寫道:

> 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+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to