0

我一直在尝试在地图上显示 JSON API 的纬度和经度,现在我已经解析了 json 值并使用 android book 创建了逐项叠加。然而,这两件事没有联系,我不确定如何将解析纬度/经度放在地图上。谁能给我一个解决方案

我正在尝试将值解析为数组并将它们显示在地图上。

我怎样才能做到这一点?

任何解决方案将不胜感激。

@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();
    }
}

我需要实现这样的目标

4

1 回答 1

1

http://www.codeproject.com/Articles/349646/Dynamic-JSON-parser

这是你想要的?这将让您解析 JSON,现在如何将它们放在地图上,我不确定,我只使用了 bingMaps Rest 服务。如果您对这个 JSON Parser 不满意,请查看一下 google。

编辑:另外,为什么要在列表中使用数组?列表非常有用,尤其是当您的数组中有未知数量的输入时。列出我的纬度 = List(); 这是一种非常有用的方法,以至于我在编写 Web 应用程序时几乎从不使用数组。

向地图添加地理点的答案:

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 地图视图的有用示例。祝你好运!

于 2012-04-17T15:53:45.340 回答