2013-06-24 63 views
2

在我的應用程序用戶鍵入一個位置,我必須獲取該位置的經度和緯度。所以在這裏我沒有Location對象,而是String如何從位置名稱獲取座標

我想到了使用此String創建Location對象的選項,但這是不可能的。

我知道使用Google的地方API是可能的,我知道如何使用它,但我更喜歡使用SDK中的任何方法。

有沒有辦法做到這一點?

回答

2

只有一個possibilty(比谷歌的地理編碼API等),如yakiv.mospan規定得到經度和緯度。但Geocoder總是返回我null許多人的情況也是如此。我通過搜索這個在stackoverflow.com上返回null的問題來了解這一點。

畢竟,我決定使用Google places API,因爲沒有其他選項可用。

+0

它在一些請求後支付 – Pulkit

-1

你可以使用這個,

Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
geocoder.getFromLocation(LATITUDE, LONGITUDE, 3); 
+0

我的目標是獲得座標。再次閱讀問題。 – Geek

3

您可以使用Geocoder。就像這樣:

Geocoder geocoder = new Geocoder(App.getContext(), Locale.US); 
    List<Address> listOfAddress; 
    try { 
     listOfAddress = geocoder.getFromLocation(theLatitude, theLongitude, 1); 
     if(listOfAddress != null && !listOfAddress.isEmpty()){ 
     Address address = listOfAddress.get(0); 

     String country = address.getCountryCode(); 
     String adminArea= address.getAdminArea(); 
     String locality= address.getLocality(); 

    } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

更新:使用此代碼段:從地址使用獲得的位置:

listOfAddress = geocoder.getFromLocationName("Address", 1); 

,並獲得緯度,經度:

double latitude = address.getLatitude(); 
double longitude = address.getLongitude(); 

更新當Geocoder返回空值時獲取位置:

private static final String URL = "http://maps.googleapis.com/maps/api/geocode/xml"; 

public static RemoteCommand getLocation(String theAddress){ 
    RemoteCommand result = new RemoteCommand(); 

    result.setType(Type.GET); 
    result.setUrl(URL); 
    result.addGetParam("address", theAddress); 
    result.addGetParam("sensor", "false"); 
    result.addGetParam("language", "en"); 

    // See description about GeocoderXMLHandler 
    result.setXmlHandler(new GeocoderXMLHandler()); 

    return result; 
} 

祝福。

+0

我的目標是獲得座標。再次閱讀問題。 – Geek

+0

已更新的答案。 –

+0

這個清單中的第一個地址是地址名稱的地址有多準確? – Geek

0

使用Google Geocoding API獲取地址的座標。 http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding

在這個細節信息如何使用給出。 下面還有示例URL,因此請檢查響應並查看這對您是否有用。

http://maps.googleapis.com/maps/api/geocode/json?address=ahmedabad&sensor=false

+0

首先,您發送的第一個鏈接是爲JavaScript?我有沒有提到JavaScript的任何地方在我的問題,我很傷心,我知道這個API。至少完全讀完這個問題。 – Geek

0

在這裏,您可以使用此代碼:

String add = address + "," + city + "," + country; 

String addressUrl = "http://maps.googleapis.com/maps/api/geocode/json?address=" 
       + URLEncoder.encode(add.replaceAll(" ", "+")) 
       + "&sensor=true"; 
try { 
      HttpClient httpClient = new DefaultHttpClient();// Client 

      HttpGet getRequest = new HttpGet(); 
      getRequest.setURI(new URI(addressUrl)); 

      // HttpPost postRequest = new HttpPost(); 
      // postRequest.setURI(new URI(url)); 

      HttpResponse response = httpClient.execute(getRequest); 
      in = new BufferedReader(new InputStreamReader(response 
        .getEntity().getContent())); 
      StringBuffer sb = new StringBuffer(""); 
      String line = ""; 
      String NL = System.getProperty("line.separator"); 
      while ((line = in.readLine()) != null) { 
       sb.append(line + NL); 
      } 
      in.close(); 
      String page = sb.toString(); 
      JSONObject jsonObject = new JSONObject(page); 
      if (jsonObject.has("results")) { 
       JSONArray jsonArray = (JSONArray) jsonObject.get("results"); 

       if (jsonArray.length() > 0) { 
        jsonObject = (JSONObject) jsonArray.get(0); 
        if (jsonObject.has("geometry")) { 
         jsonObject = (JSONObject) jsonObject 
           .get("geometry"); 

         if (jsonObject.has("location")) { 
          JSONObject location = (JSONObject) jsonObject 
            .get("location"); 

          latitude = (Double) location.get("lat"); 
          longitude = (Double) location.get("lng"); 
          Log.e(TAG, "lat : " + latitude + " long : " 
            + longitude); 
         } 
        } 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (in != null) { 
       try { 
        in.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

這裏有更多的鏈接,可以幫助你:

Google Map V2

Google Places API Demo

3
String UrlCity = "http://maps.googleapis.com/maps/api/geocode/json?address=" + NameString + "&sensor=false"; 

     JsonObjectRequest stateReq = new JsonObjectRequest(Request.Method.GET, UrlState, null, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       JSONObject location; 
       try { 
        // Get JSON Array called "results" and then get the 0th 
        // complete object as JSON 
        location = response.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location"); 
        // Get the value of the attribute whose name is 
        // "formatted_string" 
        stateLocation = new LatLng(location.getDouble("lat"), location.getDouble("lng")); 
        // System.out.println(stateLocation.toString()); 
       } catch (JSONException e1) { 
        e1.printStackTrace(); 

       } 
      } 

     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.d("Error.Response", error.toString()); 
      } 
     }); 
     // add it to the RequestQueue 
     reqQueue.add(stateReq); 

你從給定的名稱座標。這是通過使用Google Volley library完成的,或者您可以通過包裝在AsyncTask或可運行的簡單http調用來完成。 希望它有幫助!