Once again I seek the wisdom of my betters...
I'm working on an Android app that uses Google Maps and programmatically puts markers on the map based on status info from a file read from the web.
The problem I have is that each marker is drawn twice. Once in the right spot and once a little below (the top line of the second marker is in line with the bottom of the first marker).
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
try {
data = getData();
} catch (MalformedURLException e) {
//
}
if (!data.equals("")) {
String[] msg = data.split(NEWLINE);
for (Integer i = 0; i < msg.length; i++) {
items.add(new OverlayItem(
getPoint(lat.get(suburb), lng.get(suburb)),
msg[i], msg[i]));
}
}
populate();
map.postInvalidate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, false);
//boundCenterBottom(marker);
}
#Override
protected boolean onTap(int i) {
Toast.makeText(getBaseContext(),
items.get(i).getSnippet(),
Toast.LENGTH_LONG).show();
map.getController().setZoom(16 + zoomModifier);
return(true);
}
#Override
public int size() {
return(items.size());
}
}
However, the second marker only becomes evident if I uncomment the boundCenterBottom(marker) in the draw function.
I've been trying to workout what is placing the second marker for days. Can anyone point me in the right direction?
Thanks
EDIT: I forgot to mention that if I touch the screen the marker that is not boundCenterBottom disappears.
Ok, finally worked out what was wrong.
Change
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
to
public SitesOverlay(Drawable marker) {
super(marker);
boundCenterBottom(marker);
Related
Now I can show the map and markers that are defined in the program, but I need to add markers to touched location.
I think I know the way to do this, which is:
1 To set something like onTouchEventListener to the MapView;
2 Get the position information from Listener;
3 Set the marker according to the position information.
But I am too new to this to find out how to write code to achieve it, especially the step 1. I mean I don't understand how to use the solution like Add marker on touched location using google map in Android .My problem is more fundamental. I dont know how to set eventListener for my MapView and which eventListener should I use. https://developer.mapquest.com/content/mobile/android/documentation/api/com/mapquest/android/maps/Overlay.OverlayTouchEventListener.html This OverlayTouchEventListener seems to be the one but cant find an useful guide for it.
So could some one tell me how to do this in detail?
Eg.How to set the listener and which listener to choose?
Try this...
1. Create interface MyGeoPointListener.java
public interface MyGeoPointListener {
public void GetGeoPoint(GeoPoint geopoint);
}
2. Create Overlay class:
import com.mapquest.android.maps.ItemizedOverlay;
import com.mapquest.android.maps.OverlayItem;
class DynamicMarkerOverlay extends ItemizedOverlay<OverlayItem> {
private boolean isPinch = false;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private MyGeoPointListener geoPointListener;
public DynamicMarkerOverlay(Drawable defaultMarker,
MyGeoPointListener geoPointListener) {
super(boundCenterBottom(defaultMarker));
this.geoPointListener = geoPointListener;
}
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();
}
#Override
public boolean onTap(GeoPoint geoPoint, MapView map) {
if (isPinch) {
return false;
} else {
if (geoPoint != null) {
if (null != geoPointListener) {
geoPointListener.GetGeoPoint(geoPoint);
}
return true;
} else {
if (null != geoPointListener) {
geoPointListener.GetGeoPoint(null);
}
return false;
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
int fingers = event.getPointerCount();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isPinch = false; // Touch DOWN, don't know if it's a pinch yet
}
if (event.getAction() == MotionEvent.ACTION_MOVE && fingers == 2) {
isPinch = true; // Two fingers, def a pinch
}
return super.onTouchEvent(event, mapView);
}
}
3. Implement MyGeoPointListener in activity
like,
public class MainActivity extends MapActivity implements MyGeoPointListener {
4. Override MyGeoPointListener:
.....
#Override
public void GetGeoPoint(GeoPoint geopoint) {
if (null != geopoint) {
String msg = "Lat: " + geopoint.getLatitudeE6() / 1E6 + " - "
+ "Lon: " + geopoint.getLongitudeE6() / 1E6;
Toast toast = Toast.makeText(MyLocationMap.this, msg,
Toast.LENGTH_SHORT);
toast.show();
List<Overlay> mapOverlays = myMap.getOverlays();
OverlayItem overlayitem = new OverlayItem(geopoint, "address_name",
"address");
Drawable icon = getResources().getDrawable(
R.drawable.location_marker);
DynamicMarkerOverlay customoverlay = new DynamicMarkerOverlay(icon,
this);
customoverlay.addOverlay(overlayitem);
mapOverlays.add(customoverlay);
myMap.getController().animateTo(geopoint);
}
}
.....
5. In OnCreate()
......
public MapView myMap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_map_layout);
.......
.......
myMap = (MapView) findViewById(R.id.map);
Drawable marker = getResources().getDrawable(R.drawable.ic_launcher);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
myMap.getOverlays().add(new DynamicMarkerOverlay(marker, this));
......
}
6. Result:
I am a beginner when it comes to working with the Maps API so please bear with me and I know there have been many other posts dealing with the same issue but I am still stuck.
I have been able to place some overlay images onto my map. The only issue I am having now is that I do not know how to make them disappear when I zoom out enough levels as this causes the overlay images to crowd together and overlap - basicaly making them useless at that level. So, any help on how to make them appear (after zoom level 18) would be truly appreciated.
I have tried using a zoom listener and an if statement but it had no change - most likely because I do not know where exactly I need to implement it and/or what other methods are required to enable it. Also, I am not sure on how to implement the draw() method as many others have used this to make it scale and disappear.
Edit:
These are the two classes I have so far which execute successfully (after applying the answer):
The Map.java file:
public class Map extends com.google.android.maps.MapActivity implements
OnOverlayGestureListener {
private boolean mShowOverlays = true;
private MapView mMapView;
MapView mapView;
MapController mapController;
private void setOverlayVisibility() {
boolean showOverlays = mMapView.getZoomLevel() > 18;
if (showOverlays != mShowOverlays) {
mShowOverlays = showOverlays;
for (Overlay overlay : mMapView.getOverlays()) {
if (overlay instanceof ItemOverlay) {
((ItemOverlay) overlay).setVisible(showOverlays);
}
}
}
}
/** 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);
mapController = mapView.getController();
mapController.setZoom(17);
boolean showOverlays = mMapView.getZoomLevel() > 18;
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable lot = this.getResources().getDrawable(R.drawable.lot);
ItemOverlay parking_lot = new ItemOverlay(lot, this);
GeoPoint point1 = new GeoPoint(43806622, -79219797);
OverlayItem parking = new OverlayItem(point1, "Shopping Center","Parking Lot");
parking_lot.addOverlayItems(parking);
mapOverlays.add(parking_lot);
Drawable logo = this.getResources().getDrawable(R.drawable.entrance);
ItemOverlay ent = new ItemOverlay(logo, this);
GeoPoint start = new GeoPoint(43805697, -79221031);
mapController.setCenter(start);
OverlayItem welcome = new OverlayItem(start, "Welcome", " ");
ent.addOverlayItems(welcome);
mapOverlays.add(ent);
public <ZoomEvent> boolean onZoom(ZoomEvent ze, ManagedOverlay mo) {
setOverlayVisibility();
return true;
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
The ItemOverlay.java file:
public class ItemOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
private boolean visible = true;
private boolean mVisible = true;
public void setVisible(boolean value) {
mVisible = value;
}
public boolean isVisible() {
return mVisible ;
}
#Override
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
if (mVisible) {
super.draw(canvas, mapView, shadow);
}
}
public ItemOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
mContext = context;
}
public void addOverlayItems(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#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) {
// TODO Auto-generated method stub
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
I really like using the OverlayManager library for Android. It adds features to the Google Maps code, and makes a few things a lot easier. Find it here including some demo code that uses it
Option #1: If you use this, then you can use the OverlayManager's gesture listener interface for your MapActivity, to receive a callback for each zoom (in/out) event.
public class Map extends MapActivity implements OnOverlayGestureListener
{
private boolean mShowOverlays = true;
private MapView mMapView; // assign this in onCreate()
private void setOverlayVisibility() {
boolean showOverlays = mMapView.getZoomLevel() >= 18;
if (showOverlays != mShowOverlays) {
mShowOverlays = showOverlays;
for (Overlay overlay : mMapView.getOverlays()) {
if (overlay instanceof ItemOverlay) {
((ItemOverlay)overlay).setVisible(showOverlays);
}
}
}
}
// this is the onOverlayGestureListener callback:
public boolean onZoom(ZoomEvent ze, ManagedOverlay mo) {
setOverlayVisibility();
return true;
}
}
You will have to also add your Map instance as a gesture listener with ManagedOverlay.setOnOverlayGestureListener(). See the sample code for that.
Finally, in your ItemOverlay class, you can override the draw() method, and selectively draw based on whether the overlay has been marked as visible or not. You need to add a custom visible property:
public class ItemOverlay extends ItemizedOverlay {
private boolean mVisible = true;
public void setVisible(boolean value) {
mVisible = value;
}
public boolean isVisible() {
return mVisible ;
}
#Override
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
if (mVisible) {
super.draw(canvas, mapView, shadow);
}
}
}
Option #2: Now, using the Overlay Manager library just for this one purpose might be overkill. So, another, probably simpler alternative is to create a zoom listener in the way described in this stack overflow answer. The code Kurru provides would go in your Map class. You would replace this in the answer's code:
checkMapIcons();
with the method I showed above:
setOverlayVisibility();
So, now you have two ways to "watch" the zoom level, and overriding ItemOverlay.draw() allows you to make the markers disappear whenever you like (zoom level < 18 in this example).
Is there any way to put a pushpin on an android map and when it's touched displays a popup with some extra info?
You need to extend this http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/ItemizedOverlay.html.
public class CustomOverlay extends ItemizedOverlay<OverlayItem> {
private Context context;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public CustomOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
this.context = context;
//after adding things to the overlay, call these:
setLastFocusedIndex(-1);
populate();
}
#Override
protected boolean onTap(int index) {
//called when an item is tapped
return true;
}
#Override
public boolean onTap (final GeoPoint p, final MapView mapV) {
boolean tapped = super.onTap(p, mapV);
if(!tapped){
//you can use this to check for other taps on the custom elements you are drawing
}
return true;
}
#Override
public void draw(Canvas canvas, MapView mapV, boolean shadow){
if(!shadow)
// if you have a custom image you may not want the shadow to be drawn
super.draw(canvas,mapV,shadow);
if(selected != null) {
// selected just means that something was clicked
// it isn't defined in this example
Projection projection = mapV.getProjection();
Point drawPoint = projection.toPixels(selected.getPoint(), null);
//get coordinates so you can do your drawing code afterward
}
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
}
This is a very rough sketch of what you need to do. Hope this helps.
I have very limited experience with Mapview, Overlay and ItemizedOverlay. I've seen some simple code examples that use one drawable for overlays and itemizedOverlays but I'm unsure how to approach these requirements: I want more than one drawable on my map view (example: a star-icon for the map center and some other icon for other overlay items) but I want one to be unclickable (the star). Should I use both Overlay and ItemizedOverlay to achieve this?
Also, my next problem is a matter of presentation: If I have 500 items to display on a map, what is a practical way of displaying that information? Again, I have little experience developing and using map applications.
I faced the same situation a couple of weeks ago.
You should use one ItemizedOverlay for each different drawable, and then add overlayItems to the ItemizedOverlay.
The most convenient way is to extend ItemizedOverlay, so you can define a marker and a click behavoir for each style you need.
For the second part, for performance concerns, you shouldn't populate your map with all your 500 items once at a time. I used a system that dynamically adds to the map markers that belongs to the displayed scope of map.
Here is the snippet of my ItemizedOverlay that could be useful for both of your questions :
private class MarkerItemized_Overlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public MarkerItemizedOverlay(Drawable marker, Context context) {
super(boundCenterBottom(marker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
if (!mOverlays.contains(overlay)) {
mOverlays.add(overlay);
populate();
}
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(final int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setNegativeButton("Annuler",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){}
});
dialog.setPositiveButton("Naviguer",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){ showDirections(mOverlays.get(index).getPoint()); }
});
dialog.setInverseBackgroundForced(true);
dialog.show();
return true;
}
public boolean contains(Store store) {
return mOverlays.contains(store);
}
#Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
if (e.getAction() == MotionEvent.ACTION_UP) {
GeoPoint newCenter = mapView.getMapCenter();
int minLat, maxLat, minLng, maxLng;
minLat = mapCenter.getLatitudeE6() - mapView.getLatitudeSpan()/2;
maxLat = mapCenter.getLatitudeE6() + mapView.getLatitudeSpan()/2;
minLng = mapCenter.getLongitudeE6() - mapView.getLongitudeSpan()/2;
maxLng = mapCenter.getLongitudeE6() + mapView.getLongitudeSpan()/2;
if (newCenter.getLatitudeE6() > maxLat ||
newCenter.getLatitudeE6() < minLat ||
newCenter.getLongitudeE6() > maxLng ||
newCenter.getLongitudeE6() < minLng)
{
mapCenter = mapView.getMapCenter();
Location mapCenterLoc = new Location(providerName);
mapCenterLoc.setLatitude(newCenter.getLatitudeE6()/1E6);
mapCenterLoc.setLongitude(newCenter.getLongitudeE6()/1E6);
Store[] newClosestStores = storeDB.getClosestStores(mapCenterLoc);
for (int i = 0; i < newClosestStores.length; i++)
if (! itemizedOverlay.contains(newClosestStores[i]))
setMarker(newClosestStores[i]);
}
}
return super.onTouchEvent(e, mapView);
}
}
I have code for displaying two locations on the map with markers but it shows the markers only, and instead of the map it shows a blue screen only. I am running on mobile, not emulator.
My code:
public class HelloGoogleMaps3 extends MapActivity {
private MapView map=null;
private MyLocationOverlay me=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map=(MapView)findViewById(R.id.map);
map.getController().setCenter(getPoint
(40.76793169992044,-173.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker));
me=new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}
#Override
public void onResume() {
super.onResume();
me.enableCompass();
}
#Override
public void onPause() {
super.onPause();
me.disableCompass();
}
#Override
protected boolean isRouteDisplayed() {
return(false);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return(true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return(true);
}
return(super.onKeyDown(keyCode, event));
}
private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0),
(int)(lon*1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
items.add(new OverlayItem(getPoint
(40.748963847316034,-173.96807193756104),
"UN", "United Nations"));
items.add(new OverlayItem(getPoint
(40.76866299974387,-173.98268461227417),
"Lincoln Center",
"Home of Jazz at Lincoln Center"));
items.add(new OverlayItem(getPoint
(40.765136435316755,-173.97989511489868),
"Carnegie Hall",
"Where you go with practice, practice, practice"));
items.add(new OverlayItem(getPoint
(40.70686417491799,-174.01572942733765),
"The Downtown Club",
"Original home of the Heisman Trophy"));
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
#Override
protected boolean onTap(int i) {
Toast.makeText(HelloGoogleMaps3.this,
items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return(true);
}
#Override
public int size() {
return(items.size());
}
}
}
What did I do wrong?
That could mean either of the following two things:
a. Your API Key is incorrect
b. Your coordinates are set to (0, 0) and the map marker is pointing in the ocean. Try zooming the map in. You'd probably see the land. :)
This can happen if you get a location from the location manager and try and use the coordinates without first converting them:
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// location.getLatitude() is, say, 56.8751841974
// location.getLongitude() is, say, -3.9548756201
GeoPoint mapPoint = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
Basically, you need to multiply the values by 1 million to map to the correct coordinates.
You can customize the MapView color with different sets of coordinates, for example :
40.76793169992044,-173.98180484771729 is blue, as you experienced
-77.199943,11.913063 is light gray
75.913515,-30.443115 is green
43.318418,11.331644 is Siena