I am working on an Android application in which I would like to display the users immediate location, not when the location has changed. Also, I would like to mark the area with a circle as soon as location is determined by zooming in. Unfortunately, the code is only working when location is changed. I tried the code sitting on my desk, and it doesn't work, but when I am walking around, it did work. What should I do.
Here is the code :
public class MapsActivityFragment extends FragmentActivity implements LocationListener {
private MapAreaManager circleManager;
static GoogleMap googleMap;
LocationManager locationManager;
String provider;
double longitude, latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_maps_activitiy);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.googleMap);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
setUpMapIfNeeded();
onLocationChanged(location);
}
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();
if (googleMap != null) {
setupMap();
}
}
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 0, this);
}
@Override
public void onLocationChanged(Location location) {
googleMap.clear();// clean the map
Toast.makeText(this, "Location Changed", Toast.LENGTH_SHORT).show();
setUpMapIfNeeded();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
setUpMapIfNeeded();
}
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
private void setupMap() {
circleManager = new MapAreaManager(googleMap,
4, Color.RED, Color.HSVToColor(70, new float[] {1, 1, 200}), //styling
R.drawable.move, R.drawable.resize, //custom drawables for move and resize icons
0.5f, 0.5f, 0.5f, 0.5f, //sets anchor point of move / resize drawable in the middle
new MapAreaMeasure(100, MapAreaMeasure.Unit.pixels), //circles will start with 100 pixels (independent of zoom level)
new MapAreaManager.CircleManagerListener() { //listener for all circle events
@Override
public void onResizeCircleEnd(MapAreaWrapper draggableCircle) {
Toast.makeText(MapsActivityFragment.this, "do something on drag end circle: " + draggableCircle, Toast.LENGTH_SHORT).show();
}
@Override
public void onCreateCircle(MapAreaWrapper draggableCircle) {
Toast.makeText(MapsActivityFragment.this, "do something on crate circle: " + draggableCircle, Toast.LENGTH_SHORT).show();
}
@Override
public void onMoveCircleEnd(MapAreaWrapper draggableCircle) {
Toast.makeText(MapsActivityFragment.this, "do something on moved circle: " + draggableCircle, Toast.LENGTH_SHORT).show();
}
@Override
public void onMoveCircleStart(MapAreaWrapper draggableCircle) {
Toast.makeText(MapsActivityFragment.this, "do something on move circle start: " + draggableCircle, Toast.LENGTH_SHORT).show();
}
@Override
public void onResizeCircleStart(MapAreaWrapper draggableCircle) {
Toast.makeText(MapsActivityFragment.this, "do something on resize circle start: " + draggableCircle, Toast.LENGTH_SHORT).show();
}
@Override
public void onMinRadius(MapAreaWrapper draggableCircle) {
Toast.makeText(MapsActivityFragment.this, "do something on min radius: " + draggableCircle, Toast.LENGTH_SHORT).show();
}
@Override
public void onMaxRadius(MapAreaWrapper draggableCircle) {
Toast.makeText(MapsActivityFragment.this, "do something on max radius: " + draggableCircle, Toast.LENGTH_LONG).show();
}
});
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 6));
}
}
I have changed a bit of circle marking and using this library for the same. Any help would be nice. Thanks a lot.