Sample Code - Update Google Map using Location listener
Senin, 16 Juli 2012
0
komentar
Sample Code - Update Google Map using Location listener
Implement the basic example of google map as explained in the following post.
This post will focus on implementing the location listener and update the location in google map.
You need to add the following code for the location listener in the class extending the MapActivity class (see the GMapSample class from the previous sample)
private final LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// A new location update is received. Do something useful with it.
Log.i(TAG,"Location Listener start");
String coordinates[] = {""+location.getLatitude(), ""+location.getLongitude()};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mapController.animateTo(p);
mapController.setZoom(7);
mapView.invalidate();
}
@Override
public void onProviderDisabled(String provider) {
Log.i(TAG,"Location disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.i(TAG,"Location enabldes");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
Now add a location manager object in class as follows
private LocationManager locMgr;
Add the following lines of highlighted code in the onCreate Method to register the location listener with location manager.
private LocationManager locMgr;
Add the following lines of highlighted code in the onCreate Method to register the location listener with location manager.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gsample);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//register the location listener with location manager
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
mapController = mapView.getController();
mapController.animateTo(point);
mapController.setZoom(7);
mapView.invalidate();
}
The eclipse will not automatically add the import required for location APIs
Now when you run the application you can see the following output.
To send the location data to the emulator for testing you can use the DDMS tool
select the emulator device and go to the Emulator Control. See the Location Controls and update the location data and click send to send the location data to the emulator running the application.
If you like the post then please provide your feedback.
Thanks
Creative Android Apps
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
Now when you run the application you can see the following output.
To send the location data to the emulator for testing you can use the DDMS tool
DDMS Tool |
If you like the post then please provide your feedback.
Thanks
Creative Android Apps
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Sample Code - Update Google Map using Location listener
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://apk-xda.blogspot.com/2012/07/sample-code-update-google-map-using.html. Terima kasih sudah singgah membaca artikel ini.Ditulis oleh Unknown
Rating Blog 5 dari 5
0 komentar:
Posting Komentar