Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ccabanero/6996756 to your computer and use it in GitHub Desktop.
Save ccabanero/6996756 to your computer and use it in GitHub Desktop.
Android - using LocationListener with Google Maps v2
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class YourActivity extends Activity implements LocationListener {
GoogleMap googleMap;
LocationManager locationManager;
String locationProvider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set the layout
setContentView(R.layout.yourlayoutxml);
//initialize the map
if (this.googleMap == null) {
this.googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
if (this.googleMap != null) {
this.initializeMap();
}
}
//initialize the locaiton manager
this.initializeLocationManager();
}
@Override
protected void onResume() {
super.onResume();
Log.i("called", "Activity --> onResume");
this.locationManager.requestLocationUpdates(this.locationProvider, 400, 1, this);
}
@Override
protected void onPause() {
super.onPause();
Log.i("called", "Activity --> onPause");
this.locationManager.removeUpdates(this);
}
//----------------------------------------
// Summary: For initializing the map
//----------------------------------------
private void initializeMap() {
//set map type
this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//TODO: other map initialization as needed
}
//-------------------------------------------
// Summary: initialize location manager
//-------------------------------------------
private void initializeLocationManager() {
//get the location manager
this.locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//define the location manager criteria
Criteria criteria = new Criteria();
this.locationProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(locationProvider);
//initialize the location
if(location != null) {
onLocationChanged(location);
}
}
//------------------------------------------
// Summary: Location Listener methods
//------------------------------------------
@Override
public void onLocationChanged(Location location) {
Log.i("called", "onLocationChanged");
//when the location changes, update the map by zooming to the location
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(),location.getLongitude()));
this.googleMap.moveCamera(center);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
this.googleMap.animateCamera(zoom);
}
@Override
public void onProviderDisabled(String arg0) {
Log.i("called", "onProviderDisabled");
}
@Override
public void onProviderEnabled(String arg0) {
Log.i("called", "onProviderEnabled");
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
Log.i("called", "onStatusChanged");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment