I’m trying to create an application that uses OSMDroid and mapoverlays with geoPoints from the xml file, but i have a problem. Code editor doesn’t show me an error or warning but when i run the application in emulator it shows that application has stopped working.
This the code of mapActivity:
public class MapsActivity extends Activity implements LocationListener, MapViewConstants {
private MapView mapView;
private MapController mapController;
private LocationManager mLocMgr;
static final String URL = "data/data/com.siroki.brijeg/data.xml";
// XML node keys
static final String KEY_ITEM = "object"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_LON = "lon";
static final String KEY_LAT = "lat";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.copymain);
mapView = (MapView) this.findViewById(R.id.mapview);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapController = this.mapView.getController();
mapController.setZoom(14);
GeoPoint point2 = new GeoPoint(43.3803, 17.5981);
mapController.setCenter(point2);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, this);
mapView.invalidate();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable marker = this.getResources().getDrawable(R.drawable.ic_launcher);
ObjectsOverlay itemizedOverlay = new ObjectsOverlay(marker, null);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_LON, "Rs." + parser.getValue(e, KEY_LON));
map.put(KEY_LAT, parser.getValue(e, KEY_LAT));
// adding HashList to ArrayList
menuItems.add(map);
GeoPoint geo = new GeoPoint(Double.parseDouble(parser.getValue(e, KEY_LAT)), Double.parseDouble(parser.getValue(e, KEY_LON)));
OverlayItem overlayitem = new OverlayItem("Hellow", "World", geo);
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}
}
public void onLocationChanged(Location location) {
double lat = 43.3803;
double lng = 17.5981 ;
GeoPoint gpt = new GeoPoint(lat, lng);
mapController.setCenter(gpt);
mapView.invalidate();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
ObjectsOverlay.java :
public class ObjectsOverlay extends ItemizedOverlay<OverlayItem> {
public ObjectsOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy) {
super(pDefaultMarker, pResourceProxy);
// TODO Auto-generated constructor stub
}
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
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();
}
@Override
public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
// TODO Auto-generated method stub
return false;
}
}
And XML file data.xml:
<?xml version="1.0" encoding="UTF-8"?>
<objects>
<object>
<id>1</id>
<name>Siroki</name>
<lon>17.602985</lon>
<lat>43.374276</lat>
</object>
</objects>
I didn’t wrote the imports code becouse they take alot of space. 😀
First, the
contextdefined in yourObjectsOverlayis never being set. You should add aContextparameter in the constructor:so that the
DialogBuilderused inonTapcan have a context to create the dialog on. Without this your app will most probably crash when tapping an item.Next, you wrongly initialized the
GeoPoint, it should be done like this:GeoPointcoordinates are not double, likeLocationcoordinates. They are int equal to coordinate * 1E6. Use it like this:And the last thing, you do
in the same for-loop. But you should actually just
addOverlayin the loop, where you add one by one object by its coordinates, and add the completeitemizedOverlayto the map after the loop.Oh and you could also add the overlay at the beginning (before the loop), and then populate it using addOverlay. The order does not matter, it only matters to add the overlay once with
mapOverlays.add.Fix that and see if errors still happen.
Regards.