2012-07-23 33 views
0

我對JAVA編程非常陌生,但我遇到了很大的麻煩,這取決於我的工作。無論如何。 好了,所以我有這個JSON這裏:從JSON獲取座標並在Android上的Google地圖上移動到他們

{ 
"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/MV5BM[email protected]@._V1._SX40_CR0,0,40,54_.jpg");  
    itemizedOverlay.addOverlay(overlayItem2);</code> 

我有一個JSONparser成立,我只是不知道如何使mapcontroller到animateTo在我的座標點,所以我希望得到一些幫助,這:-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). 
} 

謝謝你幫

+0

你似乎在這裏問兩個問題。 1)解析JSON以獲得緯度/經度。 2)動畫到該位置。儘量不要混淆,以便更好地理解。您解析JSON並將lat/long存儲在對象中,然後在動畫地圖時使用該對象。祝你好運:) – RobGThai 2012-07-23 09:05:53

+0

我是新來的Android編程,你能給我一個例子如何將緯度/長度存儲在一個對象,並在上面的代碼中使用它? – SethPDA 2012-07-23 09:13:35

回答

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(); 
+0

如何修改這一行,以便它從json獲取緯度/經度?這是我最大的問題。我對android編程很陌生,請幫忙。 'code' GeoPoint point2 = new GeoPoint((int)(44.55332946777344 * 1E6),(int)(26.07661515625 * 1E6)); 'code' – SethPDA 2012-07-23 09:12:37

+0

更新了我的答案。 – RobGThai 2012-07-23 09:32:25

+0

非常感謝你,我實際上解決了我的問題,但主要想法是基於你的代碼,所以非常感謝你:) – SethPDA 2012-07-24 12:31:37

相關問題