I have the following code and the markers are not appearing on the map at all!
private class SitesOverlay extends ItemizedOverlay<pfOverlayItem> {
private List<pfOverlayItem> items=new ArrayList<pfOverlayItem>();
//private PopupPanel panel=new PopupPanel(R.layout.popup);
public SitesOverlay() {
super(null);
items = mainOverlayArray;
populate();
}
#Override
protected pfOverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
}
#Override
public int size() {
return(items.size());
}
private Drawable getMarker(int resource) {
Drawable marker=getResources().getDrawable(resource);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
boundCenter(marker);
return(marker);
}
}
mainOverlayArray is full of pfOverlayItem's and the code for that class is
public class pfOverlayItem extends OverlayItem {
private String coolText;
public String getcoolText() {
return coolText;
}
public void setcoolText(String coolText) {
this.coolText = coolText;
}
public pfOverlayItem(GeoPoint point, String title, String snippet) {
super(point, title, snippet);
// TODO Auto-generated constructor stub
}
}
I also set the marker outside of this after processing an XML file...
ArrayList<pfOverlayItem> overArray = myXMLHandler.getOverlayArray();
mainOverlayArray = overArray;
pfOverlayItem tempOver = null;
Drawable marker = getResources().getDrawable(R.drawable.icon);
for (int i = 0; i < mainOverlayArray.size(); i++) {
tempOver = mainOverlayArray.get(i);
tempOver.setMarker(marker);
}
sites=new SitesOverlay();
myMapView.getOverlays().add(sites);
myMapView.invalidate(); [/code]
It looks as though you're starting from one of my many sample Google Map applications. Your code as shown here is incomplete (e.g., according to the code here, you never create any OverlayItem instances).
My recommendation is you roll back to one of the samples I link to above and start modifying from there, or you start trying to figure out which of your methods are getting called and which are not.
Related
I am trying to add the marker dynamically (not static). Dynamically meaning, adding markers as and when I get updates from server.
So, following is my code for ItemizedOverlay:
private class VehicleItemizedOverlay extends ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> mOverlayItems = new ArrayList<OverlayItem>();
public VehicleItemizedOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy) {
super(pDefaultMarker, pResourceProxy);
}
#Override
public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
return false;
}
#Override
protected OverlayItem createItem(int pos) {
return mOverlayItems.get(pos);
}
#Override
public int size() {
if(mOverlayItems!=null) return mOverlayItems.size();
else return 0;
}
public void addOverlayItem(double longitude, double latitude, Bitmap bitmap){
if(Preferences.DEBUG) Log.d("MapActivity", "Latitude: "+latitude+" Longitude: "+longitude);
MarkerDrawable drawable = new MarkerDrawable(MapActivity.this, bitmap);
OverlayItem item = new OverlayItem("a", "a", new GeoPoint((int)(latitude*1e6), (int)(longitude*1e6)));
item.setMarker(drawable);
item.setMarkerHotspot(HotspotPlace.CENTER);
mOverlayItems.add(item);
populateNow();
}
public void populateNow(){
populate();
}
}
Following is the way, I am creating ItemizedOverlay & adding it to MapView in my activity onCreate():
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
mVehicleOverlay = new VehicleItemizedOverlay(getResources().getDrawable(R.drawable.stub), mResourceProxy);
mMapView.getOverlays().add(mOverlay);
Now, whenever I get response from server, I am creating & adding overlayItems to ItemizedOverlay.
//onResponse is callback I received on receiving response. Vehicle is my vehicle object
public void onResponse(ArrayList<Vehicle> aVehicles) {
for(int i=0;i<mVehicles.size();i++){
final int lat = (int)(mVehicles.get(i).getLatitude()*1e6);
final int lon = (int)(mVehicles.get(i).getLongitude()*1e6);
mVehicleOverlay.addOverlayItem(lon, lat, bitmap);
}
}
But all overlay items are overlapped over each other at center point of the map. Am I missing anything?
Also, if I add some static overlay items to mVehicleOverlay before adding it then it work properly.
mVehicleOverlay = new VehicleItemizedOverlay(getResources().getDrawable(R.drawable.stub), mResourceProxy);
final int lat = (int)(STATIC_LATITUDE*1e6);
final int lat = (int)(STATIC_LONGITUDE*1e6);
mOverlay.addOverlayItem(lon, lat, bitmap);
mMapView.getOverlays().add(mVehicleOverlay);
In OSMDroid, after adding custom itemized overlay, should I not add & populate overlayItems to it like we do in case of Google API v1? Or I am missing anything here?
IN my android map application I am trying to replace marker on touch event.This is my overlay item class and my default marker defined in main class .
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
Bitmap marker = BitmapFactory.decodeResource( null, R.drawable.image_pin);//Replace with this marker
private ArrayList<OverlayItem> myOverlaysNormal ;
Context mContext;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlaysNormal = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay){
myOverlaysNormal.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlaysNormal.get(i);
}
#Override
protected boolean onTap(int index) {
myOverlaysNormal.get(index).getPoint();
return true;
}
// Removes overlay item i
public void removeItem(int i){
myOverlaysNormal.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlaysNormal.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlaysNormal.add(overlayItem);
populate();
}
}
How can I replace touched marker with another marker(R.Drawable.image_pin)..
OR How can I expand the marker default marker?
There is multi-way to do it i hope i can help in your code but u should make good search and good read.
here some link will help u :
First Example
2 example
I've defined a map overlay, and I can display markers without issue. I'm now trying to get something to happen when I tap one, but the event never seems to fire. I'm sure I'm missing something obvious...
public class MapBlobCollection extends ItemizedOverlay<OverlayItem> {
#SuppressWarnings("serial")
public class ItemTappedEvent extends EventObject
{
public ItemTappedEvent(int itemIndex) {
super(itemIndex);
}
}
private ArrayList<OverlayItem> myOverlays ;
public MapBlobCollection(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) {
super.onTap(index);
Log.d("TESTING","Triggering tap event on " + Integer.toString(index));
EventManager.triggerEvent(this, new ItemTappedEvent(index));
return true;
}
}
Basically, the debug log entry isn't written and the event doesn't fire.
In addition, my mapview itself doesn't pan around (should it, without any extra code from me?) and despite setting the setBuitInZoomControls(true), these don't appear either... so perhaps the mapview itself is at fault?
The mapview is defined in the layout as:
<com.google.android.maps.MapView
android:id="#+id/indexMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="#string/mapskey_release"/>
And I'm not overriding any draw events or anything...
I believe you need to add
android:clickable="true"
to your mapview
Try moving the super function to the end:
#Override
protected boolean onTap(int index) {
Log.d("TESTING","Triggering tap event on " + Integer.toString(index));
EventManager.triggerEvent(this, new ItemTappedEvent(index));
return super.onTap(index);
}
So I have a mapView and I want to dynamically populate it based on a database. I can do each geopoint indidivually but for some reason when i try to do them all it doesn't display the points on the map.. If anyone has suggestions please let me know :-)
public class FieldTrip extends MapActivity {
private MapView map=null;
private MyLocationOverlay me=null;
private String theLat;
private String theLong;
private Double theDLat;
private Double theDLong;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maptastic);
map=(MapView)findViewById(R.id.mapView);
map.getController().setCenter(getPoint(40.76793169992044,
-73.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.supaaaa);
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>();
///need to make this dy-NAMIK :p
public SitesOverlay(Drawable marker) {
super(marker);
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;
theDLat = Double.parseDouble(theLat);
theDLong = Double.parseDouble(theLong);
boundCenterBottom(marker);
items.add(new OverlayItem(getPoint(theDLat,
theDLong),
"RANDOM TEXT?",
"RANDOM TEXT LALALA"));
}
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
protected boolean onTap(int i) {
Toast.makeText(FieldTrip.this,
items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return(true);
}
#Override
public int size() {
return(items.size());
}
}
}
This code looks correct to me. If you are not seeing all of the points, then fieldTripStops doesn't have all of the points you wish to display. Double check the elements in the fieldTripStops set.
I've been following the NooYawk MapView example with reasonable success. I've replaced the hardcoded geopoints and descriptions based on some system update messages. New markers are added just fine when a new update occurs. The problem is the markers don't go away when the problem is resolved.
I'd be happy enough to remove all markers on press of the refresh button as they get added back in.
Any ideas?
The below is somewhat sanitized.
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("")) {
// process data
for (Integer i = 0; i < outages.length; i++) {
items.add(new OverlayItem(
getPoint(lat, lng, headerMsg, bodyMsg));
}
}
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, false);
boundCenterBottom(marker);
}
#Override
protected boolean onTap(int i) {
Toast.makeText(getBaseContext(),
items.get(i).getSnippet(),
Toast.LENGTH_LONG).show();
return(true);
}
#Override
public int size() {
return(items.size());
}
}
on Refresh Button click mapview.getOverlays().clear(); Then start push pin in map and after adding Overlays you should mapview.postinvalidate()