我有一个大约1700个标记的文件,我试图加载到gmap v2上。在运行4.2.2的我的galaxy nexus上它加载没有问题,但是一些4.0.x和4.1.x的人没有相同的结果。他们得到地图,但没有点或应用程序在大约30秒后崩溃。我正在加载本地文件......
这是我的方法:
public void BuildMap() {
FileInputStream fXmlFile;
markerInfo = new HashMap<Marker, MapMarkers>();
try {
fXmlFile = new FileInputStream(
"/storage/emulated/0/snoteldata/kml/snotelwithlabels.kml");
XmlDom xml = new XmlDom(fXmlFile);
List<XmlDom> locations = xml.tags("Placemark");
String Name, Description, Lat, Lon;
markerInfo = new HashMap<Marker, MapMarkers>();
for (XmlDom location : locations) {
MapMarkers marks = new MapMarkers();
Name = location.tag("name").text();
Description = location.tag("description").text();
Lat = location.tag("latitude").text();
Lon = location.tag("longitude").text();
la = Float.parseFloat(Lat);
lo = Float.parseFloat(Lon);
marks.setTitle(Name);
marks.setDesc(Description);
Marker m = map.addMarker(new MarkerOptions()
.position(new LatLng(la, lo))
.title(marks.getTitle())
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.snotel_marker)));
markerInfo.put(m, marks);
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
MapMarkers markInfo = markerInfo.get(marker);
Intent i = new Intent(MainActivity.this,
MarkerInformation.class);
i.putExtra("name", markInfo.getTitle()).putExtra(
"description", markInfo.getDesc());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
}
} catch (SAXException e) {
// TODO Auto-generated catch block
Log.e("SAXException", e.getMessage());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.e("FileNotFoundException", e.getMessage());
}
}
我已经尝试将它放在AsyncTask中,但每次都得到Not on Main Thread错误...所以我不知道如何在后台运行它以保持它为人们加载直到解析完全发生。
为什么这会显示我的Gnex和Nexus 7平板电脑,但不适用于4.0.x等?如何确定问题在其他设备上的位置?
答案 0 :(得分:2)
您的代码存在两个问题。
首先,您正在主线程上读取文件。在背景中做这部分,例如使用AsyncTask返回MarkerOptions列表。迭代onPostExecute中的返回列表,将它们添加到地图中。
第二个问题可能是标记量。有几种方法可以解决这个问题。请检查此答案:Add markers dynamically on Google Maps v2 for Android
答案 1 :(得分:0)
这样做
public void BuildMap() {
final Handler mHandler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
FileInputStream fXmlFile;
markerInfo = new HashMap<Marker, MapMarkers>();
try {
fXmlFile = new FileInputStream("/storage/emulated/0/snoteldata/kml/snotelwithlabels.kml");
XmlDom xml = new XmlDom(fXmlFile);
List<XmlDom> locations = xml.tags("Placemark");
String Name, Description, Lat, Lon;
markerInfo = new HashMap<Marker, MapMarkers>();
for (XmlDom location : locations) {
final MapMarkers marks = new MapMarkers();
Name = location.tag("name").text();
Description = location.tag("description").text();
Lat = location.tag("latitude").text();
Lon = location.tag("longitude").text();
la = Float.parseFloat(Lat);
lo = Float.parseFloat(Lon);
marks.setTitle(Name);
marks.setDesc(Description);
mHandler.post(new Runnable() {
@Override
public void run() {
Marker m = map.addMarker(new MarkerOptions().position(new LatLng(la, lo)).title(marks.getTitle())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.snotel_marker)));
markerInfo.put(m, marks);
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
MapMarkers markInfo = markerInfo.get(marker);
Intent i = new Intent(MainActivity.this, MarkerInformation.class);
i.putExtra("name", markInfo.getTitle()).putExtra("description", markInfo.getDesc());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
}
});
}
} catch (SAXException e) {
// TODO Auto-generated catch block
Log.e("SAXException", e.getMessage());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.e("FileNotFoundException", e.getMessage());
}
}
}).start();
}