In my application i am using Google Maps, firstly i am displaying users current location via Marker and showing its accuracy via a transparent circle, now when i want to add another marker to the same point which represents my previous location the same code is not working for me, somebody please help me to figure out what is wrong with the code, i just want to add multiple markers within a single map view
Here's my code for displaying both the markers :-
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
//Got the location!
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
accuracy = location.getAccuracy();
cur_lati = location.getLatitude();
cur_longi = location.getLongitude();
String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0;
index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}catch (IOException e) {
e.printStackTrace();
}
Input.setPinPoints(cur_lati,cur_longi,address); // sending lat,lon to input class
Toast.makeText(getBaseContext(),"Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude() + "accuracy" + location.getAccuracy(),Toast.LENGTH_SHORT).show();
mc = mapView.getController();
mc.animateTo(point);
mc.setZoom(19);
MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(point);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
int value = bundle.getInt("tab_value");
lati = bundle.getDouble("lati");
longi = bundle.getDouble("longi");
//GeoPoint point1 = new GeoPoint((int) lati, (int) longi);
if(value == 1){
MapOverlay1 mapOverlay1 = new MapOverlay1();
// mapOverlay1.setPointToDraw(point1);
List<Overlay> listOfOverlays1 = mapView.getOverlays();
listOfOverlays1.add(mapOverlay1);
}
}
}
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
btnimg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(getApplicationContext(),Input.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_safe_city, menu);
return true;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
}
class MapOverlay extends Overlay
{
private GeoPoint pointToDraw;
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);
// convert point to pixels
Point screenPts = new Point();
Paint mPaintBorder,mPaintFill ;
mapView.getProjection().toPixels(pointToDraw, screenPts);
// add marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pinimage);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
mPaintBorder = new Paint();
mPaintBorder.setStyle(Paint.Style.STROKE);
mPaintBorder.setAntiAlias(true);
mPaintBorder.setColor(0xee4D2EFF);
mPaintFill = new Paint();
mPaintFill.setStyle(Paint.Style.FILL);
mPaintFill.setColor(0x154D2EFF);
int radius = (int) mapView.getProjection().metersToEquatorPixels(accuracy);
canvas.drawCircle(screenPts.x, screenPts.y, radius, mPaintBorder);
canvas.drawCircle(screenPts.x, screenPts.y, radius, mPaintFill);
return true;
}
}
class MapOverlay1 extends com.google.android.maps.Overlay
{
private GeoPoint p1 = new GeoPoint((int)(lati),(int)(longi));
Projection projection;
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts1 = new Point();
mapView.getProjection().toPixels(p1, screenPts1);
//---add the marker---
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.car);
canvas.drawBitmap(bmp1, screenPts1.x, screenPts1.y-50, null);
return true;
}
}
By using MapOverlay class i am able to drop a pin on map which is showing the current location of user but when i am using MapOverlay1 to show the second marker its is not working ....
any help will be appreciable thanks in advance......
Switch to the new API for google maps. It's a lot better to use. If you have to use the old API use a MyLocationOverlay for your current position. Also, if I remember correct there is a set/getMarker method couple that saves you the trouble of drawing the marker on he canvas by yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With