如何在Android App上使用Google Maps从XML文件显示坐标标记

时间:2017-04-05 15:26:12

标签: java android xml google-maps

我有一个xml文件,里面有一些地方和它们的坐标。我想在谷歌地图上的Android应用程序上显示这些地方作为标记。我已经加载了地图。 我怎么能这样做?任何帮助都会受到如此多的赞赏,即使有人能够从理论上解释它,因为我似乎无法理解它的概念。有人可以帮忙吗?

xml文件(placesp.xml)的示例:

<placesp>
<placep>
  <place_id>1</place_id>
  <name>Place1</name>
  <description>Place description 1</description>
  <coordinates>;40.430224;21.559570</coordinates>
</placep>
<placep>
  <place_id>2</place_id>
  <name>Place2</name>
  <description>Place description 2</description>
  <coordinates>;40.423324;21.062439</coordinates>
</placep>
<placep>
  <place_id>3</place_id>
  <name>Place3</name>
  <description>Place description 3</description>
  <coordinates>;40.266952;21.238220</coordinates>
</placep>
</placesp>

2 个答案:

答案 0 :(得分:0)

也许您可以使用HashMap来保存数据。 你只需创建一个这样的新类:

public class Coordinates {
public static final HashMap<String, LatLng> COORDINATES = new HashMap<String, LatLng>();
static {

    // Place1
    COORDINATES.put("Place1", new LatLng(40.430224;21.559570));
}

}

您可以像下面这样访问hashmap存储的数据:

                locationLatLng = new LatLng(Coordinates.COORDINATES.get("Place1").latitude,Coordinates.COORDINATES.get("Place1").longitude); 

然后在加载地图的类中使用此行添加标记:

map.addMarker(new MarkerOptions().position(locationLatLng));

我不确定如何从xml文件访问数据,但理论上逻辑是相同的。你必须得到一个LatLng坐标来告诉addMarker方法放置标记的位置,那就是它。我希望我可以帮助你。

答案 1 :(得分:0)

首先,您需要创建一个模型类来保存每个地方的信息。我为您提供了一个示例: Place.class

public class Place {
private int placeId;
private String placeName;
private String placeDescription;
private double placeLongitude;
private double placeLatitude;

public Place() {
    super();
}

public int getPlaceId() {
    return placeId;
}

public void setPlaceId(final int placeId) {
    this.placeId = placeId;
}

public String getPlaceName() {
    return placeName;
}

public void setPlaceName(final String placeName) {
    this.placeName = placeName;
}

public String getPlaceDescription() {
    return placeDescription;
}

public void setPlaceDescription(final String placeDescription) {
    this.placeDescription = placeDescription;
}

public double getPlaceLongitude() {
    return placeLongitude;
}

public void setPlaceLongitude(final double placeLongitude) {
    this.placeLongitude = placeLongitude;
}

public double getPlaceLatitude() {
    return placeLatitude;
}

public void setPlaceLatitude(final double placeLatitude) {
    this.placeLatitude = placeLatitude;
}
}

接下来,您将需要一个XML解析器类来将XML数据检索到Place类型列表。您可以使用以下示例: PlaceXmlParser.class

public class PlaceXmlParser {
private static final String TAG = PlaceXmlParser.class.getSimpleName();
private static final String PLACE_ID = "place_id";
private static final String PLACE_NAME = "name";
private static final String PLACE_DESCRIPTION = "description";
private static final String PLACE_COORDINATES = "coordinates";

public PlaceXmlParser() {
    super();
}

public List<Place> parsePlacesXml(final InputStream xmlStream) {
    Place place = null;
    final List<Place> placeList = new ArrayList<>();
    try {
        final XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
        final XmlPullParser parser = xmlFactoryObject.newPullParser();
        parser.setInput(xmlStream, null);
        int event = parser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            if (event == XmlPullParser.START_TAG) {
                final String name = parser.getName();
                switch (name) {
                    case PLACE_ID:
                        place = new Place();
                        setPlaceId(parser, place);
                        break;
                    case PLACE_NAME:
                        setPlaceName(parser, place);
                        break;
                    case PLACE_DESCRIPTION:
                        setPlaceDescription(parser, place);
                        break;
                    case PLACE_COORDINATES:
                        setPlaceLatLong(parser, place);
                        placeList.add(place);
                        break;
                }
            }
            event = parser.next();
        }
    } catch (final XmlPullParserException e) {
        Log.e(TAG, e.toString());
    } catch (final IOException e) {
        Log.e(TAG, e.toString());
    }
    return placeList;
}

private boolean areValidArgs(final XmlPullParser parser, final Place place) {
    return null != parser && null != place;
}

private void setPlaceId(final XmlPullParser parser, final Place place) {
    if (areValidArgs(parser, place)) {
        final String placeId = getTagValue(parser);
        place.setPlaceId(Integer.parseInt(placeId));
    }
}

private void setPlaceName(final XmlPullParser parser, final Place place) {
    if (areValidArgs(parser, place)) {
        final String placeName = getTagValue(parser);
        place.setPlaceName(placeName);
    }
}

private void setPlaceDescription(final XmlPullParser parser, final Place place) {
    if (areValidArgs(parser, place)) {
        final String placeDescription = getTagValue(parser);
        place.setPlaceDescription(placeDescription);
    }
}

private void setPlaceLatLong(final XmlPullParser parser, final Place place) {
    if (areValidArgs(parser, place)) {
        final String[] latLong = getTagValue(parser).split(";");
        if (3 == latLong.length) {
            place.setPlaceLatitude(Double.parseDouble(latLong[1]));
            place.setPlaceLongitude(Double.parseDouble(latLong[2]));
        }
    }
}

private String getTagValue(final XmlPullParser parser) {
    String result = "";
    try {
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
    } catch (final XmlPullParserException e) {
        Log.e(TAG, e.toString());
    } catch (final IOException e) {
        Log.e(TAG, e.toString());
    }
    return result;
}

}

最后,在Google Map的活动中,实现OnMapReadyCallback接口,覆盖onMapReady方法并将地点标记添加到Google Map: MapActivity.class

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private List<Place> placeList;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    this.placeList = getPlaceList();
}

@Override
public void onMapReady(final GoogleMap googleMap) {
    this.mMap = googleMap;
    addPlaceListMarkersToGoogleMap();
}

private void addPlaceListMarkersToGoogleMap() {
    for (final Place place : this.placeList) {
        final LatLong latLong = new LatLong(place.getPlaceLatitude(), place.getPlaceLongitude());
        this.mMap.addMarker(new MarkerOptions().position(latLong).title(place.getPlaceName()));
    }
}

private List<Place> getPlaceList() {
    final String xmlString = "<placesp>" +
            "<placep>" +
            "  <place_id>1</place_id>" +
            "  <name>Place1</name>" +
            "  <description>Place description 1</description>" +
            "  <coordinates>;40.430224;21.559570</coordinates>" +
            "</placep>" +
            "<placep>" +
            "  <place_id>2</place_id>" +
            "  <name>Place2</name>" +
            "  <description>Place description 2</description>" +
            "  <coordinates>;40.423324;21.062439</coordinates>" +
            "</placep>" +
            "<placep>" +
            "  <place_id>3</place_id>" +
            "  <name>Place3</name>" +
            "  <description>Place description 3</description>" +
            "  <coordinates>;40.266952;21.238220</coordinates>" +
            "</placep>" +
            "</placesp>";
    final InputStream xmlStream = getXmlStream(xmlString);
    final PlaceXmlParser parser = new PlaceXmlParser();
    return parser.parsePlacesXml(xmlStream);
}

private InputStream getXmlStream(final String xmlString) {
    InputStream xmlStream = null;
    try {
        xmlStream = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return xmlStream;
}
}

提供的代码适用于给定的XML示例,请注意可能的异常并处理它。希望这有帮助!