2017-08-15 39 views
0

我發現Google Maps API的Android Library的Place類遺漏了很多Web服務API中可用的數據。Google Places:Android API:Place class缺少一些數據

的Android類: https://developers.google.com/android/reference/com/google/android/gms/location/places/Place

網絡服務回覆: https://developers.google.com/places/web-service/details?hl=en

例如,類定義必須一無所知評論或營業時間。爲什麼是這樣?是否有可能解決它而無需將所有應用程序轉換爲Web服務API調用和json解析?也許有能力將Place對象轉換爲原始的JSON形式來查找一些額外的數據?在Android的

+0

最後決定避免在數據不完整的情況下使用Google Android API – Vadim

回答

0

Places API的唯一的,如果你想你要調用Web服務API的更多詳細信息返回類似地名,經緯度等幾個細節。這是URL

https://maps.googleapis.com/maps/api/place/details/json?key={API_KEY}&placeid={PLACE_ID} 

這裏PLACE_ID將得到放置對象在Android的Places API的

輸出示例:

{ 
                   "html_attributions" : [], 
                   "result" : { 
                    "address_components" : [ 
                    { 
                     "long_name" : "7167", 
                     "short_name" : "7167", 
                     "types" : [ "street_number" ] 
                    }, 
                    { 
                     "long_name" : "Sunset Boulevard", 
                     "short_name" : "Sunset Blvd", 
                     "types" : [ "route" ] 
                    }, 
                    { 
                     "long_name" : "Central LA", 
                     "short_name" : "Central LA", 
                     "types" : [ "neighborhood", "political" ] 
                    }, 
                    { 
                     "long_name" : "Los Angeles", 
                     "short_name" : "Los Angeles", 
                     "types" : [ "locality", "political" ] 
                    }, 
                    { 
                     "long_name" : "Los Angeles County", 
                     "short_name" : "Los Angeles County", 
                     "types" : [ "administrative_area_level_2", "political" ] 
                    }, 
                    { 
                     "long_name" : "California", 
                     "short_name" : "CA", 
                     "types" : [ "administrative_area_level_1", "political" ] 
                    }, 
                    { 
                     "long_name" : "United States", 
                     "short_name" : "US", 
                     "types" : [ "country", "political" ] 
                    }, 
                    { 
                     "long_name" : "90046", 
                     "short_name" : "90046", 
                     "types" : [ "postal_code" ] 
                    } 
                    ], 
                    "adr_address" : "\u003cspan class=\"street-address\"\u003e7167 Sunset Blvd\u003c/span\u003e, \u003cspan class=\"locality\"\u003eLos Angeles\u003c/span\u003e, \u003cspan class=\"region\"\u003eCA\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e90046\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUSA\u003c/span\u003e", 
                    "formatted_address" : "7167 Sunset Blvd, Los Angeles, CA 90046, USA", 
                    "geometry" : { 
                    "location" : { 
                     "lat" : 34.098341, 
                     "lng" : -118.346263 
                    }, 
                    "viewport" : { 
                     "northeast" : { 
                      "lat" : 34.0995122302915, 
                      "lng" : -118.3449154697085 
                     }, 
                     "southwest" : { 
                      "lat" : 34.0968142697085, 
                      "lng" : -118.3476134302915 
                     } 
                    } 
                    }, 
                    "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png", 
                    "id" : "7143e0743707f66cfa1cda945779f18e8350060e", 
                    "name" : "7167 Sunset Blvd", 
                    "place_id" : "ChIJn_0bqti-woARRtpNvG7kuq4", 
                    "reference" : "CmRbAAAAuU6CDhPuLqrS9F4XCZIV61wveIeU44RMua7FSCZHtwJeC58_OJksQvLI9Nudb9-N8RhfJDUECkxvdB-llxe6WIqrkDQMBs2vz3mBky0DH70jIcFS7f0_7yivdsxmmLzaEhAtwgzDs0va2soGuFyzuNTGGhR-xVrIvyeZwY0OGDmEp3FJmrbbeQ", 
                    "scope" : "GOOGLE", 
                    "types" : [ "street_address" ], 
                    "url" : "https://maps.google.com/?q=7167+Sunset+Blvd,+Los+Angeles,+CA+90046,+USA&ftid=0x80c2bed8aa1bfd9f:0xaebae46ebc4dda46", 
                    "utc_offset" : -420, 
                    "vicinity" : "Los Angeles" 
                   }, 
                   "status" : "OK" 
                  } 

樣品JSON解析Android中

String postalCode=""; 
        String city=""; 
        String state=""; 
        String address=""; 

        JSONObject jsonObj=new JSONObject(rs); 
        JSONArray addressComponents = jsonObj.getJSONObject("result").getJSONArray("address_components"); 
        for(int i = 0; i < addressComponents.length(); i++) { 
         JSONArray typesArray = addressComponents.getJSONObject(i).getJSONArray("types"); 
         for (int j = 0; j < typesArray.length(); j++) { 
          if (typesArray.get(j).toString().equalsIgnoreCase("postal_code")) { 
           postalCode = addressComponents.getJSONObject(i).getString("long_name"); 
          }else if (typesArray.get(j).toString().equalsIgnoreCase("locality")) { 
           city = addressComponents.getJSONObject(i).getString("long_name"); 
          }else if (typesArray.get(j).toString().equalsIgnoreCase("administrative_area_level_1")) { 
           state = addressComponents.getJSONObject(i).getString("long_name"); 
          }else { 
           String types=typesArray.get(j).toString(); 
           if(types.equalsIgnoreCase("street_number") 
             ||types.equalsIgnoreCase("route") 
             ||types.equalsIgnoreCase("neighborhood") 
             ||types.equalsIgnoreCase("sublocality_level_1") 

             ) 
           address+=addressComponents.getJSONObject(i).getString("long_name")+", "; 
          } 
         } 
        } 
+0

如您所知Google Android API更便宜(使用量更大)。你知道他們是否打算擴展自己的Android庫嗎?任何官方頻道的新聞等? – Vadim

+0

沒有關於此的任何消息。可能他們會更新他們的地點和方向API庫。 –