All About Android-Sample Code - Update Google Map using Location listener-ANDROIDKAWE

All About Android-Sample Code - Update Google Map using Location listener-ANDROIDKAWE - Welcome to ANDROID KAWE, Ini adalah sebuah blog directory tentang burung,baik itu suara burung ataupun tips perawatan burung.Blog ini adalah situs perangkum otomatis dari pencarian google yang kali ini berjudul All About Android-Sample Code - Update Google Map using Location listener-ANDROIDKAWE.Ada ribuan artikel yang sudah terekam ke dalam situs ini,silahkan cari sesuai yang kalian kehendaki,untuk artikel selanjutnya tentang judul diatas bisa kalian baca di bawah ini.

Konten : All About Android-Sample Code - Update Google Map using Location listener-ANDROIDKAWE
Judul : All About Android-Sample Code - Update Google Map using Location listener-ANDROIDKAWE

lihat juga


All About Android-Sample Code - Update Google Map using Location listener-ANDROIDKAWE

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.



@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


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 Android
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



Demikianlah Artikel All About Android-Sample Code - Update Google Map using Location listener-ANDROIDKAWE

Semoga artikel tentang All About Android-Sample Code - Update Google Map using Location listener-ANDROIDKAWE, mudah-mudahan bisa memberi manfaat untuk anda semua.

Anda sedang membaca artikel All About Android-Sample Code - Update Google Map using Location listener-ANDROIDKAWE dan artikel ini url permalinknya adalah http://androidkawe.blogspot.com/2012/07/all-about-android-sample-code-update.html Semoga artikel ini bisa bermanfaat.Sekali lagi,ini adalah situs auto yang tidak ditulis langsung oleh admin,Kami tidak menjamin akan kebenaran dari artikel yang tertulis.
Back To Top