2015-12-08 2 views
-3

Я сделал «Активность Google Maps» в Android Studio, но для добавления маркеров я использую JSON. (мое первое время с использованием JSON)Android Studio - данные JSON

В основном, когда открываются карты google, он загружает «маркер» в местоположении (String strJson), который содержит данные. Маркер отображает имя и страну, но я не могу заставить работать «latlng». Я пытался ...

Int latlng = Integer.parseInt(jsonObject.optString("latlng").toString()); 

Но «gMap.addMarker» не позволяет «Int»

Кто-нибудь знает, как я мог это сделать?

КОД:

String strJson= "{\"Location\" :[{\"name\":\"Gladiator Fitness\",\"latlng\":[\"53.2286362\",\"-0.5856869\"],\"country\":\"GB\"}]}"; 

try { 
    JSONObject jsonRootObject = new JSONObject(strJson); 
    //Get the instance of JSONArray that contains JSONObjects 
    JSONArray jsonArray = jsonRootObject.optJSONArray("Location"); 

    //Iterate the jsonArray and print the info of JSONObjects 
    for(int i = 0; i < jsonArray.length(); i++){ 
     JSONObject jsonObject = jsonArray.getJSONObject(i); 
     //Search the database for the "data" 
     String name = jsonObject.optString("name").toString(); 
     String country = jsonObject.optString("country").toString(); 
     //Add marker for each location 
     gMap.addMarker(new MarkerOptions().position(new LatLng(53.2286362,-0.5856896)).title(name).snippet(country)); 
    } 

Here скриншот того же кода. Спасибо заранее.

+0

показать нам da coooode – Blundell

+0

Да, пожалуйста, введите код непосредственно в вопрос. – ToothlessRebel

ответ

0
String strJson= "{\"Location\" :[{\"name\":\"Gladiator Fitness\",\"latlng\":[\"53.2286362\",\"-0.5856869\"],\"country\":\"GB\"}]}\n"; 

    try { 
     JSONObject jsonRootObject = new JSONObject(strJson); 
     JSONArray jsonArray = jsonRootObject.optJSONArray("Location"); 

     //Iterate the jsonArray and print the info of JSONObjects 
     for (int i = 0; i < jsonArray.length(); i++) { 
      JSONObject jsonObject = jsonArray.getJSONObject(i); 
      //Search the database for the "data" 
      String name = jsonObject.optString("name").toString(); 
      String country = jsonObject.optString("country").toString(); 
      JSONArray latlng = jsonObject.optJSONArray("latlng"); 
      double lat = latlng.optDouble(0); 
      double lng = latlng.optDouble(1); 

      Log.d("JSON", "coords: " + lat + " " + lng); 
     } 
    } catch (Exception e) { 
     Log.d("JSON", "e: " + e); 
    } 
+0

не работал, если я не ошибаюсь:/ –

+0

обновлено: этот код работает – mbmc

+0

О, хорошо, да, он работает! благодаря! но как я могу использовать это, чтобы нарисовать местоположение «маркера»? Я попытался ... gMap.addMarker (новая позиция MarkerOptions(). (Новый LatLng (jsonObject.getJSONArray ("lat"). GetDouble (0), jsonObject.getJSONArray ("lng"). GetDouble (1))) .title (имя) .snippet (страна)); –

Смежные вопросы