从JSON获取坐标并在Android上的Google地图中移至它们

时间:2012-07-23 08:45:35

标签: android json google-maps

我对JAVA编程很陌生,但我遇到了大麻烦,这取决于我的工作。无论如何。 好的,我在这里有这个json:

{
"GetPOIByTypeResult": [
    {
        "Address": {
            "City": "Bucuresti",
            "ID": 1,
            "Number": 3,
            "Street": "Otopeni"
        },
        "ID": 1,
        "IsSpecial": true,
        "Latitude": 44.542869567871094,
        "Logo": "6fb43083ee663364ef6771293653e56b.jpg",
        "Longitude": 26.06893539428711,
        "Name": "Spitalul Judetean",
        "Phone": "085228291",
        "Remark": "Normal Hospital",
        "Schedule": "Monday to Friday",
        "Specialities": [
            {
                "ID": 1,
                "Name": "Stomatologie"
            },
            {
                "ID": 2,
                "Name": "Radiologie"
            },
            {
                "ID": 3,
                "Name": "Cardiologie"
            },
            {
                "ID": 4,
                "Name": "Ginecologie"
            },
            {
                "ID": 5,
                "Name": "Pediatrie"
            },
            {
                "ID": 6,
                "Name": "Patologie"
            },
            {
                "ID": 7,
                "Name": "Oncologie"
            },
            {
                "ID": 8,
                "Name": "Macelarie"
            },
            {
                "ID": 9,
                "Name": "Oftalmologie"
            },
            {
                "ID": 10,
                "Name": "Ghipsologie"
            }
        ],
        "Type": "Hospital"
    },

我需要从JSON获取纬度和经度,并在我的应用程序中显示谷歌地图上的位置。 我在这里有一个小代码片段,它可以很好地使用本地坐标,比如输入我自己的数字,但我希望它能从JSON中获取它。

drawable = getResources().getDrawable(R.drawable.marker);
    itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);
GeoPoint point2 = new GeoPoint((int)(44.55332946777344*1E6),(int)(26.07666015625*1E6));
    CustomOverlayItem overlayItem2 = new CustomOverlayItem(point2, "Bucuresti Spital 2", 
            "(Spitalul 2)", 
            "http://ia.media-imdb.com/images/M/MV5BMzk2OTg4MTk1NF5BMl5BanBnXkFtZTcwNjExNTgzNA@@._V1._SX40_CR0,0,40,54_.jpg");       
    itemizedOverlay.addOverlay(overlayItem2);</code>

我有一个JSONparser设置,我只是不知道如何让mapcontroller动画到我的坐标所在的位置,所以我想得到一些帮助:-S

编辑:这就是我解决问题的方法,也许这对其他人也有帮助。

drawable = getResources().getDrawable(R.drawable.marker);
    itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(
            drawable, mapView); //showing marker on the map
public void showHospitalList(ArrayList<Hospitals> hospitals) {

    for (int i = 0; i < hospitals.size(); i++) {

        Hospitals hospital = hospitals.get(i);
        CustomOverlayItem temp1 = toOverlay(hospital);
        itemizedOverlay.addOverlay(temp1);
    }
    mapOverlays.add(itemizedOverlay); //this is to get the list in the activity
}

public CustomOverlayItem toOverlay(Hospitals hospitals) {

    GeoPoint point = new GeoPoint((int) (hospitals.Latitude * 1E6),
            (int) (hospitals.Longitude * 1E6));

    CustomOverlayItem temp = new CustomOverlayItem(
            point, hospitals.Name, hospitals.Phone, hospitals.Type);

    return temp;
// used the hospitals object to define the cordinates and then multiplied by 1E6 to make it compatible(from double to int)  
// Last line defines the balloon pop-up and what to show(everything is from json).
}  

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

处理JSON。您可以使用某些图书馆,如Gson或Jackson。有关详细信息:Sending and Parsing JSON Objects 。或者您可以使用JSONObject自己解析它们。

String jsonString = ... // Your JSON
List<GeoPoint> locationList = new ArrayList<GeoPoint>();
Double lat, lng; // temp
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    lat = jsonObject.getDouble("Latitude");
    lng = jsonObject.getDouble("Longtitude");
    locationList.add(
        new GeoPoint((int)(lat*1E6),(int)(lng*1E6)));
}

然后处理地图。

MapView mapView = (MapView) findViewById(R.id.map);

MapController mc = mapView.getController();
// Set Zoom level of this map (1-21).
mc.setZoom(21);
int x = 0; // Position to get location
GeoPoint point2 = locationList.get(x); // Get Object from list
// This call handle animation to move map to display the given location
mc.animateTo(point2);
mapView.postInvalidate();