Customising Android Map Balloon with multiple marker - android

In the CustomMap.java class below, i want to customised the code so that i can add arrays of geopoints, multiple itemisedOvelay as in CustomMap Part 2.
public class CustomMap extends MapActivity {
MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
// first overlay
drawable = getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);
GeoPoint point = new GeoPoint((int)(51.5174723*1E6),(int)(-0.0899537*1E6));
CustomOverlayItem overlayItem = new CustomOverlayItem(point, "Tomorrow Never Dies (1997)",
"(M gives Bond his mission in Daimler car)",
"http://ia.media-imdb.com/images/M/MV5BMTM1MTk2ODQxNV5BMl5BanBnXkFtZTcwOTY5MDg0NA##._V1._SX40_CR0,0,40,54_.jpg");
itemizedOverlay.addOverlay(overlayItem);
GeoPoint point2 = new GeoPoint((int)(51.515259*1E6),(int)(-0.086623*1E6));
CustomOverlayItem overlayItem2 = new CustomOverlayItem(point2, "GoldenEye (1995)",
"(Interiors Russian defence ministry council chambers in St Petersburg)",
"http://ia.media-imdb.com/images/M/MV5BMzk2OTg4MTk1NF5BMl5BanBnXkFtZTcwNjExNTgzNA##._V1._SX40_CR0,0,40,54_.jpg");
itemizedOverlay.addOverlay(overlayItem2);
mapOverlays.add(itemizedOverlay);
// second overlay
drawable2 = getResources().getDrawable(R.drawable.marker2);
itemizedOverlay2 = new CustomItemizedOverlay<CustomOverlayItem>(drawable2, mapView);
GeoPoint point3 = new GeoPoint((int)(51.513329*1E6),(int)(-0.08896*1E6));
CustomOverlayItem overlayItem3 = new CustomOverlayItem(point3, "Sliding Doors (1998)",
"(interiors)", null);
itemizedOverlay2.addOverlay(overlayItem3);
GeoPoint point4 = new GeoPoint((int)(51.51738*1E6),(int)(-0.08186*1E6));
CustomOverlayItem overlayItem4 = new CustomOverlayItem(point4, "Mission: Impossible (1996)",
"(Ethan & Jim cafe meeting)",
"http://ia.media-imdb.com/images/M/MV5BMjAyNjk5Njk0MV5BMl5BanBnXkFtZTcwOTA4MjIyMQ##._V1._SX40_CR0,0,40,54_.jpg");
itemizedOverlay2.addOverlay(overlayItem4);
mapOverlays.add(itemizedOverlay2);
final MapController mc = mapView.getController();
mc.animateTo(point2);
mc.setZoom(16);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Code Part 2:
This is not the code but a little logic of how i want the change to be made. To sum up, i try to make CustomMap.java flexible so that i can add multiple points and information to it throught arrays.
public class CustomMap extends MapActivity {
MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay[];
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2[];
double[]lat,lon;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
// first overlay
drawable = getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);
GeoPoint point[] = new GeoPoint((int)(lat*1E6),(int)(lon*1E6));
CustomOverlayItem overlayItem = new CustomOverlayItem(point, "Tomorrow Never Dies (1997)",
"(M gives Bond his mission in Daimler car)",
"http://ia.media-imdb.com/images/M/MV5BMTM1MTk2ODQxNV5BMl5BanBnXkFtZTcwOTY5MDg0NA##._V1._SX40_CR0,0,40,54_.jpg");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
final MapController mc = mapView.getController();
mc.animateTo(point2);
mc.setZoom(16);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}

set Overlay mapview.getOverlays().add(new SitesOverlay());
for custom popup
SitesOverlay.class
private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private List<CustomItem> items;
private View view = null;
public SitesOverlay() {
super(null);
items = new ArrayList<CustomItem>();
items.add(new CustomItem(pt, busName, "Bendigo", marker));
boundCenter(marker);
populate();
}
#Override
protected CustomItem createItem(int i) {
return (items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (!shadow) {
super.draw(canvas, mapView, false);
}
}
#Override
public int size() {
return (items.size());
}
public void refresh() {
populate();
}
public void clear() {
items.clear();
resetLastFocuesIndex();
}
public void resetLastFocuesIndex() {
setLastFocusedIndex(-1);
}
#Override
protected boolean onTap(final int index) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().removeView(view);
getMapView().invalidate();
view = null;
}
view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.balloon_main_layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
ImageView image = (ImageView) view
.findViewById(R.id.balloon_disclosure);
TextView text = (TextView) view
.findViewById(R.id.balloon_item_title);
text.setText(items.get(index).getTitle());
Projection projection = getMapView().getProjection();
Point point = new Point();
projection.toPixels(items.get(index).getPoint(), point);
int x = (int) (view.getWidth() / 2f);
int y = -marker.getIntrinsicHeight() - 3;
MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
.getPoint(), x, y + 50,
MapView.LayoutParams.BOTTOM_CENTER);
getMapView().removeView(view);
getMapView().invalidate();
getMapView().addView(view, lp);
getMapView().invalidate();
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (!items.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().invalidate();
// Intent intent=new Intent(MapLocationActivity.this,);
}
}
getMapView().invalidate();
}
});
return true;
}
}
class CustomItem extends OverlayItem {
Drawable marker = null;
CustomItem(GeoPoint pt, String name, String snippet, Drawable marker) {
super(pt, name, snippet);
this.marker = marker;
}
#Override
public Drawable getMarker(int stateBitset) {
Drawable result = marker;
setState(result, stateBitset);
return (result);
}
}
if there is multiple marker then
for (int i = 0; i < arrayList.size(); i++) {
slat = Double.parseDouble(arrayList.get(i).getLat());
vlong = Double.parseDouble(arrayList.get(i).getvLong());
pt = new GeoPoint((int) (slat * 1E6), (int) (vlong * 1E6));
Log.e("lat long", "--- "+slat);
MapviewActivity.this.mc.animateTo(pt);
items.add(new CustomItem(pt, arrayList.get(i).getBuissnessName(), "Bendigo", marker));
boundCenter(marker);
}
in SitesOverlay() constructor do this.

Related

Adding infoWindow on top of marker Google Map

I want to add an info window on top of Each marker set when user tap on a marker. Here is my code. The marker is fine i need only the infowindow appears on top of it. how can i achieve this, here is my code
public class MainActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
Drawable makerDefault = this.getResources().getDrawable(R.drawable.marker_default);
MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(makerDefault);
Drawable windmill = getResources().getDrawable(R.drawable.windmill);
Drawable bigBen = getResources().getDrawable(R.drawable.big_ben);
Drawable eiffelTower = getResources().getDrawable(R.drawable.eiffel_tower);
itemizedOverlay.addOverlayItem(52372991, 4892655, "Amsterdam", windmill);
itemizedOverlay.addOverlayItem(51501851, -140623, "London", bigBen);
itemizedOverlay.addOverlayItem(48857522, 2294496, "Paris", eiffelTower);
mapView.getOverlays().add(itemizedOverlay);
MapController mc = mapView.getController();
mc.setCenter(new GeoPoint(51035349, 2370987)); // Dunkerque, Belgium
mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlayItem(int lat, int lon, String title, Drawable altMarker) {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem, altMarker);
}
public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(OverlayItem overlayItem, Drawable altMarker) {
overlayItem.setMarker(boundCenterBottom(altMarker));
addOverlayItem(overlayItem);
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
Toast.makeText(MainActivity.this, getItem(index).getTitle(), Toast.LENGTH_LONG).show();
return true;
}
}
}
SitesOverlay.class
private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private List<CustomItem> items;
private View view = null;
public SitesOverlay() {
super(null);
items = new ArrayList<CustomItem>();
items.add(new CustomItem(pt, busName, "Bendigo", marker));
boundCenter(marker);
populate();
}
#Override
protected CustomItem createItem(int i) {
return (items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (!shadow) {
super.draw(canvas, mapView, false);
}
}
#Override
public int size() {
return (items.size());
}
public void refresh() {
populate();
}
public void clear() {
items.clear();
resetLastFocuesIndex();
}
public void resetLastFocuesIndex() {
setLastFocusedIndex(-1);
}
#Override
protected boolean onTap(final int index) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().removeView(view);
getMapView().invalidate();
view = null;
}
view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.balloon_main_layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
ImageView image = (ImageView) view
.findViewById(R.id.balloon_disclosure);
TextView text = (TextView) view
.findViewById(R.id.balloon_item_title);
text.setText(items.get(index).getTitle());
Projection projection = getMapView().getProjection();
Point point = new Point();
projection.toPixels(items.get(index).getPoint(), point);
int x = (int) (view.getWidth() / 2f);
int y = -marker.getIntrinsicHeight() - 3;
MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
.getPoint(), x, y + 50,
MapView.LayoutParams.BOTTOM_CENTER);
getMapView().removeView(view);
getMapView().invalidate();
getMapView().addView(view, lp);
getMapView().invalidate();
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (!items.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().invalidate();
// Intent intent=new Intent(MapLocationActivity.this,);
}
}
getMapView().invalidate();
}
});
return true;
}
}
class CustomItem extends OverlayItem {
Drawable marker = null;
CustomItem(GeoPoint pt, String name, String snippet, Drawable marker) {
super(pt, name, snippet);
this.marker = marker;
}
#Override
public Drawable getMarker(int stateBitset) {
Drawable result = marker;
setState(result, stateBitset);
return (result);
}
}
set Overlay mapview.getOverlays().add(new SitesOverlay());
if there is multiple marker then
for (int i = 0; i < arrayList.size(); i++) {
slat = Double.parseDouble(arrayList.get(i).getLat());
vlong = Double.parseDouble(arrayList.get(i).getvLong());
pt = new GeoPoint((int) (slat * 1E6), (int) (vlong * 1E6));
Log.e("lat long", "--- "+slat);
MapviewActivity.this.mc.animateTo(pt);
items.add(new CustomItem(pt, arrayList.get(i).getBuissnessName(), "Bendigo", marker));
boundCenter(marker);
}

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.

Showing multiple overlays in single geo point..?

I have an image to show in the overlay while clicking in the drop pin in the map. Possibly,Two or more images belongs to the same latitude and longitude. In this case, I want to show all the images belongs to that particular latitude and longitude..! How can i achieve this.?
This is what i have tried..!
for (int i = 0; i < AllData.neardeallist.size(); i++) {
mapOverlays = mapview.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.pin);
itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(
drawable, mapview);
point = new GeoPoint((int) (Double.parseDouble(AllData.neardeallist
.get(i).getLatitude()) * 1E6),
(int) (Double.parseDouble(AllData.neardeallist
.get(i).getLongitude()) * 1E6));
CustomOverlayItem overlayItem = new CustomOverlayItem(point,
StringEscapeUtils.unescapeXml(StringEscapeUtils
.unescapeHtml4(AllData.neardeallist.get(i)
.getDealTitle())),
StringEscapeUtils.unescapeXml(StringEscapeUtils
.unescapeHtml4(AllData.neardeallist.get(i)
.getDescription()))
+ ":"
+ AllData.neardeallist.get(i).getDealId()
+ ":" + AllData.neardeallist.get(i).getDealKey(),
AllData.neardeallist.get(i).getDealImg());
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
My Custom overlay class..!
public class CustomItemizedOverlay extends
BalloonItemizedOverlay {
private ArrayList<CustomOverlayItem> m_overlays = new ArrayList<CustomOverlayItem>();
private Context c;
public CustomItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(boundCenter(defaultMarker), mapView);
c = mapView.getContext();
}
public void addOverlay(CustomOverlayItem overlay) {
m_overlays.add(overlay);
populate();
}
protected CustomOverlayItem createItem(int i) {
return m_overlays.get(i);
}
public int size() {
return m_overlays.size();
}
protected boolean onBalloonTap(int i, CustomOverlayItem item) {
String[] Address = m_overlays.get(i).getSnippet().split(":");
Intent intent = new Intent(c, DealDetails.class);
intent.putExtra("deal_id", Address[1]);
intent.putExtra("deal_key", Address[2]);
c.startActivity(intent);
return true;
}
#Override
protected BalloonOverlayView<CustomOverlayItem> createBalloonOverlayView() {
// use our custom balloon view with our custom overlay item type:
return new CustomBalloonOverlayView<CustomOverlayItem>(getMapView()
.getContext(), getBalloonBottomOffset());
}
}

Cannot get touchevent from Google Maps

I'm having some issues registering when somebody has touched somewhere on the map.
I have this source
public class GMapsActivity extends MapActivity {
private MapView mapView;
private static final int latitudeE6 = 37985339;
private static final int longitudeE6 = 23716735;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
OverlayItem overlayitem = new OverlayItem(point, "Hello", "I'm in Athens, Greece!");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
//mapOverlays.add(TouchOverlay);
MapController mapController = mapView.getController();
mapController.animateTo(point);
mapController.setZoom(6);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class MapOverlap extends com.google.android.maps.Overlay {
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1){
GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY());
Toast.makeText(getBaseContext(), p.getLatitudeE6()/1E6 + "," + p.getLongitudeE6()/1E6 , Toast.LENGTH_SHORT).show();
}
return false;
}
}
}
And CustomItemizedOverlay
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
I added in the (yet it doesn't seem to do anything)
class MapOverlap extends com.google.android.maps.Overlay {
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1){
GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY());
Toast.makeText(getBaseContext(), p.getLatitudeE6()/1E6 + "," + p.getLongitudeE6()/1E6 , Toast.LENGTH_SHORT).show();
}
return false;
}
Have I implemented the class correctly? Does anyone know how to fix this?
In your second constructor, you are missing this line of code:
super(boundCenterBottom(defaultMarker));
Instead of:
this(defaultMarker);

How to add another overlay item to a android mapview

I need to add another overlay item to a mapview. I have used the standard android developers guide to google maps. I currently have the mapview within a tab. I would really appreciate any ideas. Thanks.
Below are my java classes:
public class Mapview extends MapActivity {
protected boolean isRouteDisplayed(){
return false;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// JD sports marker begin
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources()
.getDrawable(R.drawable.jd_sports_logo);
//Passes drawable(jd sports logo) into HelloItemizedOverlay class
HelloItemizedOverlay itemizedoverlay =
new HelloItemizedOverlay(drawable);
double latitude = 51.545538;
double longitude = -0.477247;
GeoPoint point = new GeoPoint((int)(latitude * 1e6),
(int)(longitude * 1e6));
OverlayItem overlayitem = new OverlayItem(point, "JD Sports",
"This is a sports shop");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
// JD sports marker ends
// Fitness first marker begin
List<Overlay> mapOverlays1 = mapView.getOverlays();
Drawable drawable1 = this.getResources()
.getDrawable(R.drawable.fitness_first_logo);
HelloItemizedOverlay itemizedoverlay1 =
new HelloItemizedOverlay(drawable);
double latitude1 = 51.545157;
double longitude1 = -0.477247;
GeoPoint point1 = new GeoPoint((int)(latitude * 1e6),
(int)(longitude * 1e6));
OverlayItem overlayitem1 = new OverlayItem(point, "JD Sports",
"This is a sports shop");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
// Fitness first marker ends
}
}
My HelloItemizedOverlay class:
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private ArrayList<OverlayItem> mOverlays1 = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
Context mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Context mContext = null;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
OverlayItem overlayitem1 = new OverlayItem(point, "JD Sports, ...
Should read
OverlayItem overlayitem1 = new OverlayItem(point1, "JD Sports", ...
Shouldn't it?
Try this:
...
Drawable drawable1 = this.getResources()
.getDrawable(R.drawable.fitness_first_logo);
boundCenterBottom(drawable1);
...

Categories

Resources