manifest :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
activity :
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
LocationListener locListener = new MyLocationListener();
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// Retrieving Latitude
location.getLatitude();
// Retrieving getLongitude
location.getLongitude();
textInfo.setText("");
String text = "My Current Location is:\nLatitude = "
+ location.getLatitude() + "\nLongitude = "
+ location.getLongitude();
textInfo.setText(text);
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT)
.show();
// set Google Map on webview
String url = "http://maps.google.com/staticmap?center="
+ location.getLatitude() + "," + location.getLongitude()
+ "&zoom=14&size=512x512&maptype=mobile/&markers="
+ location.getLatitude() + "," + location.getLongitude();
position.loadUrl(url);
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS Disabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
請先 登入 以發表留言。