Draw Circle on the Map once the application gets loaded
Posted
by
TechGeeky
on Stack Overflow
See other posts from Stack Overflow
or by TechGeeky
Published on 2012-10-18T04:36:28Z
Indexed on
2012/10/18
5:03 UTC
Read the original article
Hit count: 200
Background:-
In my application what is happening currently- Whenever I am opening the application, In the top half of the android screen, it draws a Map and in the bottom half of the android screen it show's a list view. And then as soon as the location gets changed, it draw's a Circle with the current location as the center of the circle and show's an image at the current location(center of circle). Everything is working fine till here-
Problem Statement:- What I want is when the user opens my application, circle should get draw immediately on the Google Map (this is currently not happening, it draw's circle only on the location changed), without waiting for the location to get changed and without any image on the center of circle and then if the location get's changed, take the current location as the center of circle and draw the circle with an image at the center of circle.
And this is my below code which fulfills the scenario that I mentioned in my Background- How can I make this code to work the way I wanted to? hope I am clear enough in my question. Any suggestions will be appreciated.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
listView = (ListView) findViewById(R.id.mylist);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener(mapView);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
35000,
10,
locationListener);
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(14);
}
Location Update class where I am sending the request to Overlay to draw the circle
private class GPSLocationListener implements LocationListener {
MapOverlay mapOverlay;
public GPSLocationListener(MapView mapView) {
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
mapController.animateTo(point);
mapController.setZoom(15);
if (mapOverlay == null) {
mapOverlay = new MapOverlay(this,android.R.drawable.star_on);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.add(mapOverlay);
}
mapOverlay.setPointToDraw(point);
mapView.invalidate();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Class in which circle is getting drawn.
class MapOverlay extends Overlay {
private GeoPoint pointToDraw;
int[] imageNames=new int[6];
private Point mScreenPoints;
private Bitmap mBitmap;
private Paint mCirclePaint;
public MapOverlay(GPSLocationListener gpsLocationListener, int currentUser) {
imageNames[0]=currentUser;
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setColor(0x30000000);
mCirclePaint.setStyle(Style.FILL_AND_STROKE);
mBitmap = BitmapFactory.decodeResource(getResources(),imageNames[0]);
mScreenPoints = new Point();
}
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
if (pointToDraw == null) {
return true;
}
mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);
int totalCircle=5;
int radius=40;
int centerimagesize=35;
for (int i = 1; i <= totalCircle; i ++) {
canvas.drawCircle(mScreenPoints.x,mScreenPoints.y, i*radius, mCirclePaint);
}
canvas.drawBitmap(mBitmap, (mScreenPoints.x-(centerimagesize/2)),(mScreenPoints.y-(centerimagesize/2)), null);
super.draw(canvas,mapView,shadow);
return true;
}
}
© Stack Overflow or respective owner