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
Last month, the California-based internet giant began re-evaluating its user-edited online map system after the latest embarrassing incident -- an image of an Android mascot urinating on an Apple logo. To know more about , Visit Android training in chennai
ReplyDeleteIt would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
ReplyDeleteJava interview questions and answers
Core Java interview questions and answers
Java training in Chennai | Java training in Tambaram
Java training in Chennai | Java training in Velachery
I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
ReplyDeleteexcel advanced excel training in bangalore
Devops Training in Chennai
I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
ReplyDeleteangularjs-Training in tambaram
angularjs-Training in sholinganallur
angularjs-Training in velachery
angularjs Training in chennai
angularjs-Training in pune
angularjs-Training in chennai
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeletemicrosoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training
This comment has been removed by the author.
ReplyDeleteI absolutely love your site.. Very nice colors & theme. Did you develop this amazing site yourself? Please reply back as I’m wanting to create my own personal website and would love to know where you got this from or what the theme is named. Thanks
ReplyDeleteUI Development Training In Marathahalli
Selenium Training In Bangalore
Ojalá seas siempre feliz y afortunado. Espero que tengas más buenos artículos.
ReplyDeletegiảo cổ lam giảm cân
giảo cổ lam giảm béo
giảo cổ lam giá bao nhiêu
giảo cổ lam ở đâu tốt nhất
Very informative article. I really loved the way content is written here. I have also shared this article on my social media channels.
ReplyDeleteinbound marketing agency in india
outsource website development
top digital marketing agencies in india
virtual assistant websites
The inspiration to do well in these classes will have to come from the student himself since all learning is self-paced and there will be no competition for high grades.
ReplyDeleteJava training in Chennai
Java Online training in Chennai
Java Course in Chennai
Best JAVA Training Institutes in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Training
Java Online Training
"It was an informative post indeed. Now It's the time to make the switch to solar power,
ReplyDeletecontact us(National Solar Company) today to learn more about how solar power works.
battery storage solar
united solar energy
solar panels
solar inverter
solar batteries
solar panels adelaide
best solar panels
solar power
battery storage solar
battery charger solar
solar regulators
solar charge controllers
solar battery storage
instyle solar
solar panels melbourne
solar panels for sale
solar battery charger
solar panels cost
buy solar panels"
Nice Article. Thank you for sharing the informative article with us.
ReplyDeleteAndroid Tips
Free download 10 minute school course
রবি সিমের সকল কোড ২০২২
Facebook Tricks
Techno Riad