adding multiple marker on google map in android - android

I am triyng to add multiple marker on google map. Here is my code section
public class GoogleMap extends MapView
{
MapController mc;
MapView mapView;
GeoPoint p;
#Override
public void onCreate(Bundle savedInstanceState)
{
....
double lat = Double.parseDouble(bundle.getString("paramLat"));
double lng = Double.parseDouble(bundle.getString("paramLong"));
mc = mapView.getController();
p = new GeoPoint( (int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
I have two question here. When i tried to add only one marker, it works but draw method is invoked many times. But why? and when is it invoked?
The second question is how can i add new marker. I created second geoPoint named p2 and after that what should i do? Thank you very much.

I have implemented the multiple markers in my project. Here is the sample code; some things that you need to change is the marker image, the length (number of marker you want define in the for loop). Hope this will help!!!
public class ShowMapActivity extends MapActivity{
private MapController mapControll;
private GeoPoint geoPoint=null;
private MapView mapview;
private MyItemizedOverlay userPicOverlay;
private MyItemizedOverlay nearPicOverlay;
private Drawable userPic,atmPic;
private OverlayItem nearatms[] = new OverlayItem[50];
public static Context context;
#Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
context = getApplicationContext();
setContentView(R.layout.your layout xml);
showMap();
}
public void showMap() {
// TODO Auto-generated method stub
try {
geoPoint = new GeoPoint((int)(latitude * 1E6),(int)(longitude * 1E6));
mapview = (MapView)findViewById(R.id.mapview);
mapControll= mapview.getController();
mapview.setBuiltInZoomControls(true);
mapview.setStreetView(true);
mapControll.setZoom(16);
mapControll.animateTo(geoPoint);
userPic = this.getResources().getDrawable(R.drawable.your pic);
userPicOverlay = new MyItemizedOverlay(userPic);
OverlayItem overlayItem = new OverlayItem(geoPoint, "I'm Here!!!", null);
userPicOverlay.addOverlay(overlayItem);
mapview.getOverlays().add(userPicOverlay);
atmPic = this.getResources().getDrawable(R.drawable.your pic);
nearPicOverlay = new MyItemizedOverlay(atmPic);
for (int i = 0; i < define your length here; i++) {
nearatms[i] = new OverlayItem(new GeoPoint((int)((latitude) * 1E6)),(int)(longitude) * 1E6)),"Name", null);//just check the brackets i just made change here so....
nearPicOverlay.addOverlay(nearatms[i]);
}
mapview.getOverlays().add(nearPicOverlay);
//Added symbols will be displayed when map is redrawn so force redraw now
mapview.postInvalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Itemized Class for placing the marker
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> myOverlays ;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlays = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay){
myOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlays.get(i);
}
// Removes overlay item i
public void removeItem(int i){
myOverlays.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
#Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
String title = myOverlays.get(index).getTitle();
Toast.makeText(ShowMapActivity.context, title, Toast.LENGTH_LONG).show();
return super.onTap(index);
}
}

To prevent the multiple drawing you need a cache. This is a bug in the draw method of MapOverlay
To add multiple markers you have to use ItemizedOverlay. This may help you.

You should follow the Android Map View tutorial on the developers site.
Part 2 has the section for building an Overlay.
http://developer.android.com/resources/tutorials/views/hello-mapview.html
Minimal work should be done in the Draw method; it is called a lot including everytime the map is moved/zoomed/"invalidated"

Your going to want to start with an ItemizedOverlay which is an array of points. You can find the documentation here http://code.google.com/android/add-ons/google-apis/reference/index.html . Then your going to want to invoke the ItemizedOverlay.draw() method which will draw all the points within it based on their position. Hope this helps.

Related

OnTouch on google maps removes the drag and scroll in mapview as well as zoom controls dont appear ontouch

ok as the question says I have a mapview and a OnTouchListener set to it and in the Ontouch function i set a overlay image on the map. But in this process now the zoom controls and drag functionality in the mapview is removed somehow. please help me out with it.
I have already setzoomcontrolson to true (no effect). Help please.
public class marker extends ItemizedOverlay {
Context mContext;
OverlayItem overlayitem;
Drawable marker;
private int xDragImageOffset=0;
private int yDragImageOffset=0;
private int xDragTouchOffset=0;
private int yDragTouchOffset=0;
OverlayItem inDrag;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public marker(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
marker=defaultMarker;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.clear();
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
public boolean onTap(GeoPoint geoPoint, MapView mapView){
boolean tappedAnOverlay = super.onTap(geoPoint, mapView);
if (tappedAnOverlay) {
places.gp=geoPoint; // do your thing if hit an overlay
}
else {
// no overlay found in that location
}
MapController mc=mapView.getController();
mc.animateTo(geoPoint);
return true;
}
}
this is my marker overlays file
and
public class places extends MapActivity implements android.view.View.OnClickListener/*,OnTouchListener*/ {
MapView mapView;
MapController mc;
Button srchbtn;
EditText searchstring;
marker itemizedoverlay;
public static List<Overlay> mapOverlays;
OverlayItem overlayitem;
public static GeoPoint gp;
Button back;
String s1;
String s2="yourprofile";
String s3="partnerprofile";
public int zoom=5;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.place);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
srchbtn=(Button)findViewById(R.id.searchbtn);
searchstring=(EditText)findViewById(R.id.edplnm);
srchbtn.setOnClickListener(this);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
itemizedoverlay = new marker(drawable, this);
back=(Button)findViewById(R.id.btnbk);
back.setOnClickListener(this);
s1=getIntent().getStringExtra("ClassName");
mc=mapView.getController();
mc.setZoom(zoom);
gp=new GeoPoint((int)(21.7679 * 1E6), (int)(78.8718 * 1E6));
mc.animateTo(gp);
// mapView.setOnTouchListener(this);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.searchbtn:String s = searchstring.getText().toString();
String[] vals =s.split(":");
float lat;
float logi;
lat=Float.parseFloat(vals[0]);
logi=Float.parseFloat(vals[1]);
gp = new GeoPoint((int)(lat * 1E6), (int)(logi * 1E6));
MapController mc=mapView.getController();
mc.setZoom(80);
mc.animateTo(gp);
overlayitem = new OverlayItem(gp, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
break;
case R.id.btnbk: int Lat;
int Logi;
Lat=gp.getLatitudeE6();
Logi=gp.getLongitudeE6();
boolean val=s1.equals(s2);
boolean val2=s1.equals(s3);
Intent I1=new Intent(places.this,yourprofile.class);
Intent I2=new Intent(places.this,partnerprofile.class);
if(val)
{
I1.putExtra("LAT",Lat);
I1.putExtra("LONG",Logi);
setResult(RESULT_OK,I1);
finish();
}else
if(val2)
{
I2.putExtra("LAT",Lat);
I2.putExtra("LONG",Logi);
setResult(RESULT_OK,I2);
finish();
}
break;
}
}
/*public boolean onTouch(View v, MotionEvent e) {
if(e.getAction()==MotionEvent.ACTION_UP){
Projection p =((MapView)v).getProjection();
gp = p.fromPixels((int) e.getX(), (int) e.getY());
overlayitem = new OverlayItem(gp, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mc.setZoom(zoom);
mc.animateTo(gp);
searchstring.setText(((float)gp.getLatitudeE6())/1000000 + ":" +
((float)gp.getLongitudeE6())/1000000);
}
return true;
}*/
}
this is my places class file
Here is the Sample Working Demo which you may want to Use.
It will help you to Drag&Drop the Image with Zooming and Touch Functionality for Mapview.
Hope it will Help.

Map overlay in android app

I am trying to add a pin image to certain places to my map in my android application, but then there is a big semi transparent rectangle that appears over my map, that does not allow me to interact with the main map, I have tried using map overlay or itemized map overlay, but i get the same result, I dont know if this is because of the image, or this is usually what happens when someone adds an overlay to his map.
PS I am using a .png image
If anyone could help me i would be so grateful
thanks
this is the itemized overlay class
private class MirItemizedOverlay extends ItemizedOverlay {
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MirItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
}
}
and this is how I use it in the oncreate method in my map activity
Drawable makerDefault =this.getResources().getDrawable(R.drawable.redcircle);
MirItemizedOverlay itemizedOverlay = new MirItemizedOverlay(makerDefault);
GeoPoint point = new GeoPoint((int) (49.9736518*1E6), (int) (7.114648 *1E6));
OverlayItem overlayItem = new OverlayItem(point, "hotspot1", null);
itemizedOverlay.addOverlayItem((int) (49.9736518*1E6), (int) (7.114648 *1E6), "hotspot1");
You should add your itemizedOverlay to MapView overlays:
MapView mapView = ...;
mapView.getOverlays().add(itemizedOverlay);

how to get searched position from google map in android

In my code I have one textbox and one button. Whatever place you type into the textbox, that place has to get searched from Google maps on clicking the button. And an icon has to be shown to the searched place.
In my code I am getting the map, but I am not able to search for the location. Please help.
public class MapActivity extends com.google.android.maps.MapActivity implements
OnClickListener {
/** Called when the activity is first created. */
MapView view;
Button search;
EditText location;
MapController controller;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (MapView) findViewById(R.id.themap);
location = (EditText) findViewById(R.id.editText1);
search = (Button) findViewById(R.id.search);
search.setOnClickListener(this);
view.setBuiltInZoomControls(true);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == search) {
Geocoder geo = new Geocoder(getApplicationContext(),
Locale.getDefault());
try {
List<Address> addresses = geo.getFromLocationName(location
.getText().toString(), 5);
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint((int) (addresses.get(0)
.getLatitude() * 1E6), (int) (addresses.get(0)
.getLongitude() * 1E6));
controller.animateTo(p);
controller.setZoom(12);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> lisOverlays = view.getOverlays();
lisOverlays.clear();
lisOverlays.add(mapOverlay);
} else {
AlertDialog.Builder adb = new AlertDialog.Builder(
MapActivity.this);
adb.setTitle("Google Map");
adb.setMessage("please provide proper place");
adb.setPositiveButton("Close", null);
adb.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
MapOverlay.java
public class MapOverlay extends com.google.android.maps.Overlay
{
Context context;
public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
GeoPoint p = null;
mapView.getProjection().toPixels(p, screenPts);
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
return true;
}
}
When I compile this, I am getting an error in the line: controller.animateTo(p);
In oncreate() instantiate the Mapcontroller as below so that you will not get an error in animateTo method.
controller= view.getController();
also GeoPioint p =null; so replace the MAp overlay class with below
MapOverlay.java
public class MapOverlay extends com.google.android.maps.Overlay
{
GeoPoint p;
Context context;
public MyOverLay(Context context,GeoPoint p) // GeoPoint is a int. (6E)
{
this.p = p;
this.context = context;
}
public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
return true;
}
then in your main code pass geopoint like below it will solve your problem....
MapOverlay mapOverlay = new MapOverlay(getApplicationContext(),p);

the application stopped unexpectedly while search the area in map error occur in android

I can view the map in android mobile now i put one edit text for search area. And create one button for search the place
enter code here
public class MapActivity extends com.google.android.maps.MapActivity implements
OnClickListener {
/** Called when the activity is first created. */
MapView view;
Button search;
EditText location;
MapController controller;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (MapView) findViewById(R.id.themap);
location = (EditText) findViewById(R.id.editText1);
search = (Button) findViewById(R.id.search);
search.setOnClickListener(this);
view.setBuiltInZoomControls(true);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == search) {
Geocoder geo = new Geocoder(getApplicationContext(),
Locale.getDefault());
try {
List<Address> addresses = geo.getFromLocationName(location
.getText().toString(), 5);
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint((int) (addresses.get(0)
.getLatitude() * 1E6), (int) (addresses.get(0)
.getLongitude() * 1E6));
controller.animateTo(p);
controller.setZoom(12);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> lisOverlays = view.getOverlays();
lisOverlays.clear();
lisOverlays.add(mapOverlay);
} else {
AlertDialog.Builder adb = new AlertDialog.Builder(
MapActivity.this);
adb.setTitle("Google Map");
adb.setMessage("please provide proper place");
adb.setPositiveButton("Close", null);
adb.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Mapoverlay.java
public class MapOverlay extends com.google.android.maps.Overlay {
Context context;
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
GeoPoint p = null;
mapView.getProjection().toPixels(p, screenPts);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher);
canvas.drawBitmap(bitmap, screenPts.x, screenPts.y, null);
return true;
}
}
My log cat shows error in controller.animateTo(p);
How can i solve the error in the above line.
How do i provide search option in the map.
Could someone tell me where the mistake is in the above code?
In oncreate() instantiate the Mapcontroller as below so that you will not get an error in animateTo method.
controller= view.getController();

android draw pin on google map

I have created googlemap in my android application. Now i want to draw pin at my location.
This is my code for that:-
public class GooglemapActivity extends MapActivity {
private List<Overlay> mapOverlays;
private Drawable drawable;
private MyOverlay itemizedOverlay;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mMapView;
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setBuiltInZoomControls(true);
mapOverlays = mMapView.getOverlays();
drawable = getResources().getDrawable(R.drawable.c_map_pin_blue);
itemizedOverlay = new MyOverlay(drawable);
GeoPoint point = new GeoPoint((int) 26.863966000000000,
(int) 75.766938300000000);
itemizedOverlay.addItem(point, "Quippelin", "0");
mapOverlays.add(itemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
and this is my another class:-
public class MyOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
public MyOverlay(Drawable marker) {
super(boundCenterBottom(marker));
// TODO Auto-generated constructor stub
populate();
}
public void addItem(GeoPoint p, String title, String snippet) {
OverlayItem newItem = new OverlayItem(p, title, snippet);
overlayItemList.add(newItem);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return overlayItemList.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return overlayItemList.size();
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
// boundCenterBottom(marker);
}
}
my map is displaying correctly but my pin is not displaying, please help me to findout the mistake which i have done.
Thank you in advance.
Everything looks good except your point is way "off".
Your calculation is wrong as your pin should be displayed near coordinates 0,0. Try that:
GeoPoint point = new GeoPoint((int) (26.863966 * 1E6), (int) (75.7669383 * 1E6));
Reason: your (int) cast results in 26 and 75 which should be somewhere near the north pole. Check the documentation:
Constructs a GeoPoint with the given latitude and longitude, measured in microdegrees (degrees * 1E6).
I used this code to resolve my problem.
double mLatitude = 26.863966000000000;
double mLongtitude = 75.766938300000000;
GeoPoint point = new GeoPoint((int) (mLatitude * 1E6),(int) (mLongtitude * 1E6));
Now its working fine.
and answer of WarrenFaith is also correct.
The issue is with your point. Try these points.
GeoPoint point = new GeoPoint((int) 19240000,(int) -99120000);

Categories

Resources