2012-09-17 24 views
6

20m alanındaki herhangi bir banka için Goolge Places Pages öğesini uzun/lat olarak aramam gerekiyor. Bu Google Places Doc, JavaScript ile nasıl yapılacağını açıklar. Java'da sahip olmadığım bir google.maps.LatLng Nesnesi kullanıyorlar.Java ile bir Google Rehber Arama İsteği nasıl gönderilir

Artık Hizmet'i nasıl arayacaksınız?

Belki de Goolge Places için bir Java API'si var mı?

Saygılarımızla, Christian.

Düzenleme 1:

böyle url inşa someone bulundu:

String url = baseUrl + "location=" + lat + "," + lon + "&" + 
       "radius=" + searchRadius + "&" + types + "&" + "sensor=true" + 
       "&" + "key=" + googleAPIKey; 

Cevap: Düzenleme 2: Çünkü yazının

i nasıl yapılacağını öğrendim yukarıda.

İşte
public class GooglePlacesClient 
{ 
    private static final String GOOGLE_API_KEY = "***"; 

    private final HttpClient client   = new DefaultHttpClient(); 

    public static void main(final String[] args) throws ParseException, IOException, URISyntaxException 
    { 
     new GooglePlacesClient().performSearch("establishment", 8.6668310, 50.1093060); 
    } 

    public void performSearch(final String types, final double lon, final double lat) throws ParseException, IOException, URISyntaxException 
    { 
     final URIBuilder builder = new URIBuilder().setScheme("https").setHost("maps.googleapis.com").setPath("/maps/api/place/search/json"); 

     builder.addParameter("location", lat + "," + lon); 
     builder.addParameter("radius", "5"); 
     builder.addParameter("types", types); 
     builder.addParameter("sensor", "true"); 
     builder.addParameter("key", GooglePlacesClient.GOOGLE_API_KEY); 

     final HttpUriRequest request = new HttpGet(builder.build()); 

     final HttpResponse execute = this.client.execute(request); 

     final String response = EntityUtils.toString(execute.getEntity()); 

     System.out.println(response); 
    } 

} 

cevap

13

daha eksiksiz bir örnek Rehber API'sı arama, otomatik tamamlama ve detaylar için (JSON ayrıştırma ve bazı durum işleme içerir): Bu isteği göndermek nasıl bir örnektir. Android için yazılmıştır, ancak Android dışı kullanım için kolayca yerleştirilebilir (org.json lib'lerini dahil etmek ve farklı kayıtlar kullanmak gerekir). Place sınıfı basit bir değer nesnesidir. Google Rehber API ile çalışmak için

package com.example.google.places; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 

import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.ArrayList; 

/** 
* @author saxman 
*/ 
public class PlacesService { 
    private static final String LOG_TAG = "ExampleApp"; 

    private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place"; 

    private static final String TYPE_AUTOCOMPLETE = "/autocomplete"; 
    private static final String TYPE_DETAILS = "/details"; 
    private static final String TYPE_SEARCH = "/search"; 

    private static final String OUT_JSON = "/json"; 

    // KEY! 
    private static final String API_KEY = "YOUR KEY"; 

    public static ArrayList<Place> autocomplete(String input) { 
     ArrayList<Place> resultList = null; 

     HttpURLConnection conn = null; 
     StringBuilder jsonResults = new StringBuilder(); 
     try { 
      StringBuilder sb = new StringBuilder(PLACES_API_BASE); 
      sb.append(TYPE_AUTOCOMPLETE); 
      sb.append(OUT_JSON); 
      sb.append("?sensor=false"); 
      sb.append("&key=" + API_KEY); 
      sb.append("&input=" + URLEncoder.encode(input, "utf8")); 

      URL url = new URL(sb.toString()); 
      conn = (HttpURLConnection) url.openConnection(); 
      InputStreamReader in = new InputStreamReader(conn.getInputStream()); 

      int read; 
      char[] buff = new char[1024]; 
      while ((read = in.read(buff)) != -1) { 
       jsonResults.append(buff, 0, read); 
      } 
     } catch (MalformedURLException e) { 
      Log.e(LOG_TAG, "Error processing Places API URL", e); 
      return resultList; 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "Error connecting to Places API", e); 
      return resultList; 
     } finally { 
      if (conn != null) { 
       conn.disconnect(); 
      } 
     } 

     try { 
      // Create a JSON object hierarchy from the results 
      JSONObject jsonObj = new JSONObject(jsonResults.toString()); 
      JSONArray predsJsonArray = jsonObj.getJSONArray("predictions"); 

      // Extract the Place descriptions from the results 
      resultList = new ArrayList<Place>(predsJsonArray.length()); 
      for (int i = 0; i < predsJsonArray.length(); i++) { 
       Place place = new Place(); 
       place.reference = predsJsonArray.getJSONObject(i).getString("reference"); 
       place.name = predsJsonArray.getJSONObject(i).getString("description"); 
       resultList.add(place); 
      } 
     } catch (JSONException e) { 
      Log.e(LOG_TAG, "Error processing JSON results", e); 
     } 

     return resultList; 
    } 

    public static ArrayList<Place> search(String keyword, double lat, double lng, int radius) { 
     ArrayList<Place> resultList = null; 

     HttpURLConnection conn = null; 
     StringBuilder jsonResults = new StringBuilder(); 
     try { 
      StringBuilder sb = new StringBuilder(PLACES_API_BASE); 
      sb.append(TYPE_SEARCH); 
      sb.append(OUT_JSON); 
      sb.append("?sensor=false"); 
      sb.append("&key=" + API_KEY); 
      sb.append("&keyword=" + URLEncoder.encode(keyword, "utf8")); 
      sb.append("&location=" + String.valueOf(lat) + "," + String.valueOf(lng)); 
      sb.append("&radius=" + String.valueOf(radius)); 

      URL url = new URL(sb.toString()); 
      conn = (HttpURLConnection) url.openConnection(); 
      InputStreamReader in = new InputStreamReader(conn.getInputStream()); 

      int read; 
      char[] buff = new char[1024]; 
      while ((read = in.read(buff)) != -1) { 
       jsonResults.append(buff, 0, read); 
      } 
     } catch (MalformedURLException e) { 
      Log.e(LOG_TAG, "Error processing Places API URL", e); 
      return resultList; 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "Error connecting to Places API", e); 
      return resultList; 
     } finally { 
      if (conn != null) { 
       conn.disconnect(); 
      } 
     } 

     try { 
      // Create a JSON object hierarchy from the results 
      JSONObject jsonObj = new JSONObject(jsonResults.toString()); 
      JSONArray predsJsonArray = jsonObj.getJSONArray("results"); 

      // Extract the Place descriptions from the results 
      resultList = new ArrayList<Place>(predsJsonArray.length()); 
      for (int i = 0; i < predsJsonArray.length(); i++) { 
       Place place = new Place(); 
       place.reference = predsJsonArray.getJSONObject(i).getString("reference"); 
       place.name = predsJsonArray.getJSONObject(i).getString("name"); 
       resultList.add(place); 
      } 
     } catch (JSONException e) { 
      Log.e(LOG_TAG, "Error processing JSON results", e); 
     } 

     return resultList; 
    } 

    public static Place details(String reference) { 
     HttpURLConnection conn = null; 
     StringBuilder jsonResults = new StringBuilder(); 
     try { 
      StringBuilder sb = new StringBuilder(PLACES_API_BASE); 
      sb.append(TYPE_DETAILS); 
      sb.append(OUT_JSON); 
      sb.append("?sensor=false"); 
      sb.append("&key=" + API_KEY); 
      sb.append("&reference=" + URLEncoder.encode(reference, "utf8")); 

      URL url = new URL(sb.toString()); 
      conn = (HttpURLConnection) url.openConnection(); 
      InputStreamReader in = new InputStreamReader(conn.getInputStream()); 

      // Load the results into a StringBuilder 
      int read; 
      char[] buff = new char[1024]; 
      while ((read = in.read(buff)) != -1) { 
       jsonResults.append(buff, 0, read); 
      } 
     } catch (MalformedURLException e) { 
      Log.e(LOG_TAG, "Error processing Places API URL", e); 
      return null; 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "Error connecting to Places API", e); 
      return null; 
     } finally { 
      if (conn != null) { 
       conn.disconnect(); 
      } 
     } 

     Place place = null; 
     try { 
      // Create a JSON object hierarchy from the results 
      JSONObject jsonObj = new JSONObject(jsonResults.toString()).getJSONObject("result"); 

      place = new Place(); 
      place.icon = jsonObj.getString("icon"); 
      place.name = jsonObj.getString("name"); 
      place.formatted_address = jsonObj.getString("formatted_address"); 
      if (jsonObj.has("formatted_phone_number")) { 
       place.formatted_phone_number = jsonObj.getString("formatted_phone_number"); 
      } 
     } catch (JSONException e) { 
      Log.e(LOG_TAG, "Error processing JSON results", e); 
     } 

     return place; 
    } 
} 
+0

Hayatımı kurtardığın için teşekkür ederim. :) – Kunal

+0

Çok fazla thnks !!! –

10

bir Java kütüphanesi GitHub'dan ve Maven Merkez mevcuttur (açıklama: Ben geliştiriciyim.) Yerlerde (veya detaylar, fotoğraf, vs) bir listesini alma can Bir veya iki satırda yapılmalıdır. Örnekler ve kurulum detayları için proje sayfasına bakın.

https://github.com/pushbit/sprockets

+0

havalı, teşekkür ederim :) – d0x

+0

Bu harika, bunun için teşekkürler :) – Alex

1

Google Rehber API için kullanılabilir herhangi bir resmi bir Java kütüphanesi var yok. Bununla birlikte, Github'da barındırılan birçok proje var. Bir diğeri: Google Places API Java Library on Github

+1

Bağlantılı havuzun orijinal projenin bir çatalı olduğu unutulmamalıdır. Orijinal repo [burada] bulunur (https://github.com/windy1/google-places-api-java) – Windwaker

+0

Var. https://github.com/googlemaps/google-maps-services-java –

İlgili konular