只显示一个标记

时间:2014-01-13 19:27:32

标签: android xml google-maps

即使我的XML中有2个坐标,我的地图上也只显示一个标记。我错过了什么?

我的XML

<?xml version="1.0" encoding="utf-8"?>
<markers>
<marker> 
<lat>51.615325</lat>
<lng>0.524572</lng>
<icon>flat</icon>
</marker>
<marker><lat>51.60002</lat>
<lng>0.518862</lng>
<icon>house</icon>
</marker>
</markers>

我的解析代码

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
import android.util.Xml;

public class ParserParsing {

private static final String ns = null;
public ArrayList<Entry> parse(InputStream in)
{ 
ArrayList<Entry> list = null;

try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
list=readFeed(parser);
for(int i=0;i<list.size();i++)
{
Log.i(".......",list.get(i).lat);
Log.i(".......",list.get(i).lng);
Log.i(".......",list.get(i).icon);
}
} catch(Exception e){

}
return list;
}
private  ArrayList<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {

ArrayList<Entry> entry= new ArrayList<Entry>();
parser.require(XmlPullParser.START_TAG, ns, "markers");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
Log.i("............",name);
// Starts by looking for the entry tag
if (name.equals("marker")) {
entry.add(readMarker(parser));

} else {
skip(parser);
}
}  
return entry;
}
private Entry readMarker(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "marker");
String lat = null;
String lng = null;
String icon =null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
Log.i("............",name);
if (name.equals("lat")) {
lat = readLat(parser);
} else if (name.equals("lng")) {
lng = readLng(parser);
else if (name.equals("icon")) {
icon = readIcon(parser);
} 
else {
skip(parser);
}
}
return new Entry(lat,lng,icon);

}
private String readLat(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "lat");
String lat = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "lat");
return lat;
}
private String readLng(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "lng");
String lng = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "lng");
return lng;
}
private String readIcon(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "icon");
String icon = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "icon");
return icon;
}


private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}    
}

我知道这里有一些我忽略但却无法弄清楚是什么


01-13 19:37:41.157: D/SSRMv2:Monitor(694): SIOP:: AP = 220, Prev AP = 220, Duration = 10070
01-13 19:37:43.680: D/SSRMv2:Monitor(694): SIOP:: AP = 220 (read only)
01-13 19:37:45.301: D/AndroidRuntime(11095): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
01-13 19:37:45.301: D/AndroidRuntime(11095): CheckJNI is OFF
01-13 19:37:45.301: D/AndroidRuntime(11095): setted country_code = UK
01-13 19:37:45.301: D/AndroidRuntime(11095): setted countryiso_code = GB
01-13 19:37:45.311: D/AndroidRuntime(11095): setted sales_code = VOD
01-13 19:37:45.311: D/AndroidRuntime(11095): readGMSProperty: start
01-13 19:37:45.311: D/AndroidRuntime(11095): readGMSProperty: already setted!!
01-13 19:37:45.311: D/AndroidRuntime(11095): readGMSProperty: end
01-13 19:37:45.311: D/AndroidRuntime(11095): addProductProperty: start
01-13 19:37:45.321: D/dalvikvm(11095): Trying to load lib libjavacore.so 0x0
01-13 19:37:45.331: D/dalvikvm(11095): Added shared lib libjavacore.so 0x0
01-13 19:37:45.341: D/dalvikvm(11095): Trying to load lib libnativehelper.so 0x0
01-13 19:37:45.341: D/dalvikvm(11095): Added shared lib libnativehelper.so 0x0
01-13 19:37:45.631: D/dalvikvm(11095): Note: class Landroid/app/ActivityManagerNative; has 176 unimplemented (abstract) methods
01-13 19:37:45.712: D/AndroidRuntime(11095): Calling main entry com.android.commands.pm.Pm
01-13 19:37:45.732: D/AndroidRuntime(11095): Shutting down VM
01-13 19:37:45.742: D/dalvikvm(11095): Debugger has detached; object registry had 1 entries
01-13 19:37:46.262: D/SSRMv2:Monitor(694): SIOP:: AP = 220 (read only)
01-13 19:37:46.292: D/AndroidRuntime(11106): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
01-13 19:37:46.292: D/AndroidRuntime(11106): CheckJNI is OFF
01-13 19:37:46.292: D/AndroidRuntime(11106): setted country_code = UK
01-13 19:37:46.292: D/AndroidRuntime(11106): setted countryiso_code = GB
01-13 19:37:46.292: D/AndroidRuntime(11106): setted sales_code = VOD
01-13 19:37:46.292: D/AndroidRuntime(11106): readGMSProperty: start
01-13 19:37:46.292: D/AndroidRuntime(11106): readGMSProperty: already setted!!
01-13 19:37:46.292: D/AndroidRuntime(11106): readGMSProperty: end
01-13 19:37:46.292: D/AndroidRuntime(11106): addProductProperty: start
01-13 19:37:46.302: D/dalvikvm(11106): Trying to load lib libjavacore.so 0x0
01-13 19:37:46.312: D/dalvikvm(11106): Added shared lib libjavacore.so 0x0
01-13 19:37:46.322: D/dalvikvm(11106): Trying to load lib libnativehelper.so 0x0
01-13 19:37:46.322: D/dalvikvm(11106): Added shared lib libnativehelper.so 0x0
01-13 19:37:46.622: D/dalvikvm(11106): Note: class Landroid/app/ActivityManagerNative; has 176 unimplemented (abstract) methods
01-13 19:37:46.692: D/AndroidRuntime(11106): Calling main entry com.android.commands.am.Am
01-13 19:37:46.743: I/power(694): *** acquire_dvfs_lock : lockType : 1  freq : 1350000 
01-13 19:37:46.743: D/SSRMv2:CustomFrequencyManagerService(694): acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 1350000  uid : 1000  pid : 694  pkgName : ACTIVITY_RESUME_BOOSTER@6
01-13 19:37:46.753: W/ActivityManager(694): mDVFSHelper.acquire()
01-13 19:37:46.763: D/WindowManager(694): computeScreenConfigurationLocked() set config.orientation=1 dw=1080 dh=1920 Callers=com.android.server.wm.WindowManagerService.updateOrientationFromAppTokensLocked:5171 com.android.server.wm.WindowManagerService.updateOrientationFromAppTokens:5142 com.android.server.am.ActivityStack.realStartActivityLocked:1018 
01-13 19:37:46.763: D/PowerManagerService(694): setKeyboardVisibility: false
01-13 19:37:46.773: E/MP-Decision(1743): num online cores: 1 reqd : 2 available : 4 rq_depth:1.400000 hotplug_avg_load_dw: 70
01-13 19:37:46.773: E/MP-Decision(1743): UP cpu:1 core_idx:1 Nw:1.900000 Tw:140 total_time_up:0.000000
01-13 19:37:46.773: D/SurfaceWidgetClient(24114): [123491/11] SurfaceWidgetClient onTrimMemory level: 20
01-13 19:37:46.783: D/LockPatternUtils(694): isPcwEnable = null
01-13 19:37:46.783: D/AndroidRuntime(11106): Shutting down VM
01-13 19:37:46.783: D/jdwp(11106): Got wake-up signal, bailing out of select
01-13 19:37:46.783: D/dalvikvm(11106): Debugger has detached; object registry had 1 entries
01-13 19:37:46.823: D/AbsListView(10561): Get MotionRecognitionManager
01-13 19:37:46.823: I/SurfaceFlinger(206): id=4904 Removed Mauncher (0/5)
01-13 19:37:46.823: D/AbsListView(10561): onVisibilityChanged() is called, visibility : 8
01-13 19:37:46.823: D/AbsListView(10561): unregisterIRListener() is called 

标记代码

InputStream is = null;
try {
is = this.getResources().getAssets().open("markers.xml");
} catch (IOException e) {

e.printStackTrace();
}

ArrayList<Entry> entries= (ArrayList<Entry>) new ParserParsing().parse(is);
Double latitude = Double.valueOf(entries.get(0).lat);
Double longitude = Double.valueOf(entries.get(0).lng);
String icon = entries.get(0).icon;
LatLng lng = new LatLng(latitude,longitude);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.the_map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(lng)
.title(icon)
.icon(BitmapDescriptorFactory.fromResource(getResources().getIdentifier(icon, "drawable", getPackageName()))));

1 个答案:

答案 0 :(得分:1)

更改为

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.the_map)).getMap();
ArrayList<Entry> entries= (ArrayList<Entry>) new ParserParsing().parse(is);
for(int i=0;i>entries.size();i++)
{
Double latitude = Double.valueOf(entries.get(i).lat);
Double longitude = Double.valueOf(entries.get(i).lng);
String icon = entries.get(i).icon;
LatLng lng = new LatLng(latitude,longitude);
mMap.addMarker(new MarkerOptions()
.position(lng)
.title(icon)
.icon(BitmapDescriptorFactory.fromResource(getResources().getIdentifier(icon, "drawable", getPackageName()))));
}