我想获得一些关于获得lat和lon的帮助。我的意思是:我从网上解析了lat和lon(使用XMLPullParser)。现在,我最大的问题是获取所有值(lat和lon)并在地图上绘制它们。请帮助!
这是我的Parser课程:
public class Parser {
//Feed Parsing Method
public ArrayList<Bakery> parse(String url) {
//Array of Episode Objects
ArrayList<Bakery> bakeries = null;
try {
//Encode the URL into a URL Object
URL bakery_feed_url = new URL(url);
//Open a Connection to the feed
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(bakery_feed_url.openConnection().getInputStream(), null);
} finally {
}
int event_type = parser.getEventType();
Bakery current_bakery = null;
boolean done = false;
//Parse the feed, start reading throughout the feed from top to bottom
while (event_type != XmlResourceParser.END_DOCUMENT && !done){
String tag_name = null;
/*
lat = Integer.parseInt(parser.getAttributeValue(null,"lat"));
lon = Integer.parseInt(parser.getAttributeValue(null,"lon"));
grade = Integer.parseInt(parser.getAttributeValue(null,"grade"));
*/
switch (event_type){
//Found the start of the feed
case XmlResourceParser.START_DOCUMENT:
bakeries = new ArrayList<Bakery>();
break;
//Found a start tag
case XmlResourceParser.START_TAG:
//apply the data to our Episode object based on the tag name
tag_name = parser.getName();
if(tag_name.equalsIgnoreCase("place")){
current_bakery = new Bakery();
}else if(current_bakery != null){
if (tag_name.equalsIgnoreCase("phone_number")){
current_bakery.setPhone(parser.nextText());
}else if(tag_name.equalsIgnoreCase("city")){
current_bakery.setCity(parser.nextText());
}else if(tag_name.equalsIgnoreCase("province")){
current_bakery.setState(parser.nextText());
}else if(tag_name.equalsIgnoreCase("address")){
current_bakery.setAddress(parser.nextText());
}else if(tag_name.equalsIgnoreCase("lat")){
current_bakery.setLatitude(parser.nextText());
//double lat = Integer.parseInt(parser.getAttributeValue(null,"lat"));
// current_bakery.setLater(Integer.parseInt(parser.getAttributeValue(null, "lat")));
}else if(tag_name.equalsIgnoreCase("postal_code")){
current_bakery.setZip(parser.nextText());
}else if(tag_name.equalsIgnoreCase("lng")){
current_bakery.setLongitude(parser.nextText());
}else if(tag_name.equalsIgnoreCase("name")){
current_bakery.setPlace_name(parser.nextText());
}
}
break;
//An end tag has been reached
case XmlResourceParser.END_TAG:
tag_name = parser.getName();
//End of an Episode Item
if (tag_name.equalsIgnoreCase("place") && current_bakery != null){
bakeries.add(current_bakery);
//Reached the end of all bakeries, no more data to collect
} else if (tag_name.equalsIgnoreCase("places")){
done = true;
}
break;
}
event_type = parser.next();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
//Return the Episode Array
return bakeries;
}
}
如你所见,我正在解析lat和lon好吧,但是再次,因为我将它全部作为字符串,我遇到麻烦将它转换为int然后将其提供给OverlayItems类。我希望我不会让这太复杂。
答案 0 :(得分:3)
我像这样使用ItemizedOverlay:
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import com.google.android.maps.OverlayItem;
public class ItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public csItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public csItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
dialog.show();
return true;
}
}
然后当我需要将这些项目应用到叠加层时,我会这样做:
public void drawOverlay(MapView mapView) {
List<Overlay> mapOverlays = mapView.getOverlays();
mapOverlays.clear();
mapView.invalidate();
int zoomLevel = mapView.getZoomLevel();
Drawable drawable = this.getResources().getDrawable(R.drawable.myDrawable);
ItemizedOverlay itemizedoverlay[] = new ItemizedOverlay[myList.size()];
for (int i = 0; i < myList.size(); i++) {
Foo foo = myList.get(i);
GeoPoint point = new GeoPoint((int)(foo.lattitude * 1E6),
(int)(foo.longtitude * 1E6));
OverlayItem overlayItem = new OverlayItem(point, foo.titel, foo.description);
itemizedoverlay[i].addOverlay(overlayItem);
mapOverlays.add(itemizedoverlay[i]);
}
}
希望这就是你要找的东西。