我一直在尝试在地图上显示JSON API的纬度和经度,现在我已经解析了json值并使用android book创建了逐项覆盖。然而这两件事没有连接,我不知道如何将解析lat / long放到地图上。任何人都可以给我一个解决方案
我正在尝试将值解析为数组并将其显示在地图上。
我怎样才能做到这一点?
任何解决方案都将不胜感激。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// mapView.setStreetView(true);
// mapView.isStreetView();
// mapView.isSatellite();
JSONArray properties = null;
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
properties = json.getJSONArray(TAG_PROPERTY);
// looping through All Contacts
for (int i = 0; i < properties.length(); i++) {
JSONObject c =
properties.getJSONObject(i);
String lat = c.getString(TAG_LATITUDE);
String longi = c.getString(TAG_LONGITUDE);
System.out.println(lat);
}
} catch (Exception e) {
}//getting JSOn values
Drawable marker=getResources().getDrawable(R.drawable.mapmarker);
marker.setBounds((int)(-marker.getIntrinsicWidth()/2),
-marker.getIntrinsicHeight(),
(int) (marker.getIntrinsicWidth()/2),
0);
InterestingLocations funPlaces = new InterestingLocations(marker);
mapView.getOverlays().add(funPlaces);
GeoPoint pt = funPlaces.getCenterPt();
double latSpan = funPlaces.getLatSpanE6();
double lonSpan = funPlaces.getLonSpanE6();
Log.v("Overlays", "Lat span is " + latSpan);
Log.v("Overlays", "Lon span is " + lonSpan);
MapController mc = mapView.getController();
mc.setCenter(pt);
mc.zoomToSpan((int)(latSpan*1.5), (int)(lonSpan*1.5));
}
@Override
protected boolean isLocationDisplayed() {
return false;
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
class InterestingLocations extends ItemizedOverlay {
private ArrayList<OverlayItem> locations = new ArrayList<OverlayItem>();
private GeoPoint center = null;
我需要使用对象数组替换下面的代码
public InterestingLocations(Drawable marker)
{
super(marker);
// create locations of interest
GeoPoint Maddox = new
GeoPoint((int)(51.513292002080306*1000000),(int)(-0.1420965932139256*1000000));
GeoPoint WoburnPlace = new
GeoPoint((int)(51.52457273349151*1000000),(int)(-0.12765430497584865*1000000));
GeoPoint Capstansquare = new
GeoPoint((int)(51.49858590609855*1000000),(int)(-0.007696115597470304*1000000));
GeoPoint Devonshire = new
GeoPoint((int)(51.52222533547292*1000000),(int)(-0.1440394063396748*1000000));
GeoPoint Bolsover = new
GeoPoint((int)(51.52266783118477*1000000),(int)(-0.14358896505772278*1000000));
GeoPoint Haymarket = new
GeoPoint((int)(51.51000027761305*1000000),(int)(-0.13300746016819762*1000000));
locations.add(new OverlayItem(Haymarket,
"Seven Seas Lagoon", "Seven Seas Lagoon"));
locations.add(new OverlayItem(Bolsover,
"Seven Seas Lagoon", "Seven Seas Lagoon"));
locations.add(new OverlayItem(Devonshire,
"Seven Seas Lagoon", "Seven Seas Lagoon"));
locations.add(new OverlayItem(Capstansquare,
"Seven Seas Lagoon", "Seven Seas Lagoon"));
locations.add(new OverlayItem(WoburnPlace,
"Seven Seas Lagoon", "Seven Seas Lagoon"));
locations.add(new OverlayItem(Maddox,
"Magic Kingdom", "Magic Kingdom"));
populate();
}
// We added this method to find the middle point of the cluster
// Start each edge on its opposite side and move across with each point.
// The top of the world is +90, the bottom -90,
// the west edge is -180, the east +180
public GeoPoint getCenterPt() {
if(center == null) {
int northEdge = -90000000; // i.e., -90E6 microdegrees
int southEdge = 90000000;
int eastEdge = -180000000;
int westEdge = 180000000;
Iterator<OverlayItem> iter = locations.iterator();
while(iter.hasNext()) {
GeoPoint pt = iter.next().getPoint();
if(pt.getLatitudeE6() > northEdge) northEdge = pt.getLatitudeE6();
if(pt.getLatitudeE6() < southEdge) southEdge = pt.getLatitudeE6();
if(pt.getLongitudeE6() > eastEdge) eastEdge = pt.getLongitudeE6();
if(pt.getLongitudeE6() < westEdge) westEdge = pt.getLongitudeE6();
}
center = new GeoPoint((int)((northEdge + southEdge)/2),
(int)((westEdge + eastEdge)/2));
}
return center;
}
@Override
public void draw(Canvas canvas, MapView mapview, boolean shadow) {
// Here is where we can eliminate shadows by setting to false
super.draw(canvas, mapview, shadow);
}
@Override
protected OverlayItem createItem(int i) {
return locations.get(i);
}
@Override
public int size() {
return locations.size();
}
}
我需要实现this
之类的东西答案 0 :(得分:1)
http://www.codeproject.com/Articles/349646/Dynamic-JSON-parser
这是你在找什么?这将让你解析JSON,现在如何将它们放在地图上,我不确定,我只使用了bingMaps Rest服务。如果你对这个JSON Parser不满意,请稍微浏览谷歌。
编辑:另外,为什么列表上的数组?列表非常有用,特别是如果您的阵列中有未知数量的输入。 列出myLatitude = List(); 这是一个非常有用的方法,我几乎从不在编写Web应用程序时使用Arrays。向地图添加地理位置的答案:
GeoPoint point2 = new GeoPoint(35410000, 139460000);
OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
有关叠加效果的详细信息,请参阅此链接下的第2部分: http://developer.android.com/resources/tutorials/views/hello-mapview.html 这里有很多关于如何使用Android地图视图的有用示例。祝你好运!