I have a map activity that has many pins of map, and when I click a pin, a custom balloon opens, showing some information about that pin. Also, I have a search bar, where if you type the name of a knob, the info appears there, but I want it to go to that searched pin.
Example: on the map you have different vegetables pins, and when you search carrot, the search list will show the element carrot, and when you click on it, the balloon for the carrot pin will inflate. So, my question is : is there some sort of OnTap() void method ? I know, that OnTap(int index) returns a boolean.
create your own itemized overlay, and override the onTap method, and in your main class, make an instance of the itemized overlay, and call overlay.onTap(point)
Sample code:
public class MyItemizedOverlay<Item> extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> m_overlays;
private MapView mapView;
final MapController mc;
public MyItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(boundCenterBottom(defaultMarker), mapView);
m_overlays = new ArrayList<OverlayItem>();
mc = mapView.getController();
populate();
}
public void addOverlay(OverlayItem overlay) {
m_overlays.add(overlay);
setLastFocusedIndex(-1);
populate();
}
public ArrayList<OverlayItem> getOverlays() {
return m_overlays;
}
public final boolean onTap(int index) {
GeoPoint point;
point = createItem(index).getPoint();
mc.animateTo(point);
return true;
}
...
}
In the main class
public class Main extends MapActivity {
private MapView mapView;
private List<Overlay> mapOverlays;
private MyItemizedOverlay overlay;
private Drawable pin;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
doAction();
}
private void doAction() {
mapView = (MapView)findViewById(R.id.map_view);
pin = res.getDrawable(R.drawable.pin);
overlay = new MyItemizedOverlay(pin, mapView);
GeoPoint point = new GeoPoint((int)(7*1E6),(int)(42*1E6));
overlayItem = new OverlayItem(point, "title", "text");
overlay.addOverlay(overlayItem);
mapOverlays = mapView.getOverlays();
mapOverlays.add(overlay);
//we tap the point here
overlay.onTap(0);
}
}
Related
I'm trying to write a simple MapView using Google Maps API v1 (Targeting Gingerbread devices)
Just tried following the example hello-mapview and it all works apart from the image does not get displayed on the map.
There is an overlay, as the touch even works and displays the text, how ever no image appears with it.
Can anyone spot what I have missed?
MyMapView.java
public class MyMapView extends MapActivity {
private MapView mapView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = getResources().getDrawable(R.drawable.androidmarker);
NewOverlay itemizedoverlay = new NewOverlay(drawable, this);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
NewOverlay.java
public class NewOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public NewOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlays(){
mOverlays.clear();
populate();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
So I'm just trying to create a generic mapView in android and put a few geopoints in it. I've followed the instructions from the android developer website and had no success... can anyone help? here is my code.
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
// defines the bounds for the overlayItems
super(boundCenterBottom(defaultMarker));
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
// returns the correct ArrayList position from int i
return mOverlays.get(i);
}
#Override
public int size() {
// returns number of items in ArrayList
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
//Here's my main class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maptastic);
((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
setTitle("Field Trip");
Drawable drawable = this.getResources().getDrawable(R.drawable.supaaaa);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
//initControls();
MapView mapview = (MapView) findViewById(R.id.mapView);
mapview.setBuiltInZoomControls(true);
mcontroller = mapview.getController();
// Plot all geo points
for (Entry<Integer, FieldTripStop> cur : Statics.fieldTripStops.entrySet())
{
// get the FieldTripStop object from the current hash table entry
Statics.currentFTStop = cur.getValue();
// concatenate numbers before (all) and after (6) the decimal, since
// geopoints only accept 6 numbers past the decimal.
theLat = Statics.currentFTStop.latitude;
theLong = Statics.currentFTStop.longitude;
/*
* String manipulation method
*
* theLat = theLat.replace(".","");
* theLat = theLat.substring(0, 8);
* theLong = theLong.replace(".","");
* theLong = theLong.substring(0, 8);
* point = new GeoPoint((int)(Integer.valueOf(theLat)), (int)(Integer.valueOf(theLong)));
*/
theDLat = Double.parseDouble(theLat);
theDLong = Double.parseDouble(theLong);
//olay = new OverlayItem(point, "herp", "derp");
//olayitems.add(olay);
List<Overlay> mapOverlays = mapview.getOverlays();
point = new GeoPoint((int)(theDLat*1E6), (int)(theDLong*1E6));
OverlayItem overlayitem = new OverlayItem(point, "holda, Mundo!", "I'm in Mexico City~!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
You need to a) use only a single HelloItemizedOverlay (instead one for each point) and b) add this overlay to the map before you add the markers (In your current code, the overlay is not yet associated with the map when populate is called, so the map does not receive any notification and doesn't refresh itself).
Some things to check:
Did you specify your api key for the MapView? *
*) How to specify your api key (in the layout resource for your activity):
<com.google.android.maps.MapView android:id="#+id/map"
...
android:apiKey="your-api-key-here"/>
I want to display few circles in google maps on my android application.
I want that when user clicks these circle it should show a toast based on the circle clicked.
I am using code.google.android.maps.overlay to display circle on a specific lat/long.
I am unable to find a solution.
Extend the ItemizedOverlay class
public class MapItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Drawable myPic;
private Activity mapActivity;
public MapItemizedOverlay(Drawable defaultMarker, Activity context) {
super(boundCenterBottom(defaultMarker));
this.mapActivity = context;
this.myPic = defaultMarker;
}
protected boolean onTap(int index) {
OverlayItem item = mOberlays.get(index);
... //Toast code
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
}
this is a class which handles overlayitems.
There you can implement the onTap()-Method and show Toasts.
In your MapActivity you simply create this MapitemizedOverlay and add your items.
MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(circleDrawable, this);
I'm trying to add an overlay for myLocation in Android. The map displays, but the overlay does not. I did get the overlay to appear using a separate class that extends ItemizedOverlay. I'm wondering of there is a way to display this individual point without creating a separate class?
Attached is the source code for the activity class.
public class WalkAbout extends MapActivity {
//for Hello_mapview
List<Overlay> mapOverlays;
Drawable drawable;
private MapView m_vwMap;
private MapController m_mapController;
private PathOverlay m_pathOverlay;
private MyLocationOverlay m_locationOverlay;
private ArrayList<GeoPoint> m_arrPathPoints;
private ArrayList<OverlayItem> m_arrPicturePoints;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initLocationData();
initLayout();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
private void initLocationData() {
// TODO
}
private void initLayout() {
//instantiate XML File into corresponding view objects.
//Then inflate new view from XML resource.
setContentView(R.layout.map_layout);
MapView m_vwMap = (MapView)findViewById(R.id.mapview);
m_vwMap.setBuiltInZoomControls(true);
m_vwMap.setSatellite(true);
//retrieve list of overlay objects
mapOverlays=m_vwMap.getOverlays();
//set market for overlays
drawable=this.getResources().getDrawable(R.drawable.item);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
//create OverlayItem with my location
m_locationOverlay= new MyLocationOverlay(this, m_vwMap);
//enable market to set location and direction
m_locationOverlay.enableCompass();
m_locationOverlay.enableMyLocation();
mapOverlays.add(m_locationOverlay);
}
}
You need to call invalidate() on your m_vwMap so it will redraw itself.
I followed the instructions from the google hellomapview tutorial. I get a working mapview etc. But the two items that are added to the map are not shown. It seems they are there somewhere because tapping at the specified location shows the message that was added to the items.
Edit
Here is my source code. It should be very close to the google tutorial source code.
public class MapOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> overlays = new ArrayList<OverlayItem>();
private Context context;
public MapOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
overlays = new ArrayList<OverlayItem>();
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return overlays.get(i);
}
#Override
public int size() {
return overlays.size();
}
public void addOverlay(OverlayItem overlay) {
overlays.add(overlay);
this.populate();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = overlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
public class MapsActivity extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
MapOverlay overlay = new MapOverlay(this.getResources().getDrawable(
R.drawable.androidmarker), this);
overlay.addOverlay(new OverlayItem(new GeoPoint(19240000,-99120000), "Blubb", "See?"));
mapView.getOverlays().add(overlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Is the source code from the google tutorial available somewhere?
The problem is that I forgot to set the bounds of the drawable. It seems that if the mapview doesn't know how to align the image it won't show it at all.
I changed the first line in my constructor from:
super(defaultMarker);
to
super(boundCenterBottom(defaultMarker));
and know its working perfect.
At the same time, I have no idea how to help you directly.
Here are links to various editions of a project that definitely work with overlays, perhaps they will help.