我尝试使用xml文件中的geoPoints创建一个使用OSMDroid和mapoverlay的应用程序,但是我遇到了问题。代码编辑器没有向我显示错误或警告,但是当我在模拟器中运行应用程序时,它显示应用程序已停止工作。 这是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;
}
}
和XML文件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>
我没有写过进口代码,因为他们占用了很多空间。 :d
答案 0 :(得分:1)
首先,context
中定义的ObjectsOverlay
永远不会被设置。您应该在构造函数中添加Context
参数:
public ObjectsOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy, Context context) {
super(pDefaultMarker, pResourceProxy);
this.context = context;
}
以便DialogBuilder
中使用的onTap
可以有一个上下文来创建对话框。如果没有这个,你的应用程序很可能在点击项目时崩溃。
接下来,您错误地初始化了GeoPoint
,应该这样做:
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
GeoPoint
坐标不加倍,如Location
坐标。它们 int 等于坐标* 1E6。像这样使用它:
int lon = (int)Math.round(lon_as_double*1E6)
最后一件事,你做了
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
在同一个for循环中。但实际上你应该在循环中只有addOverlay
,你可以通过坐标逐个添加对象,并在循环后将完整的itemizedOverlay
添加到地图中。
哦,您还可以在开头(循环之前)添加叠加层,然后使用addOverlay填充它。订单无关紧要,只需使用mapOverlays.add
添加叠加层一次。
修复此问题并查看错误是否仍然存在。
问候。