2011-05-11 8 views
0

私は特定の場所の緯度と経度がある場合、その場所が特定の状態にあることをどのように確認できますか?緯度と経度の位置を確認する方法

例: latとlongがSanta Claraを参照している場合は、それがカリフォルニアにあることをどのように確認できますか?

おかげ

答えて

0

ジオコーダーとアドレスクラスを使用してソリューションを達成します。このコードはuが...すべての最高の...

Geocoder gc = new Geocoder(this, Locale.getDefault()); 
    try { 
    List<Address> addresses = gc.getFromLocation(lat, lng, 1); 
    StringBuilder sb = new StringBuilder(); 
    if (addresses.size() > 0) { 
     Address address = addresses.get(0); 

     for (int i = 0; i < address.getMaxAddressLineIndex(); i++) 
      String addr=address.getAddressLine(i); 
      //use this addr string and compare . u will get the solution 

    } 

    } catch (IOException e) {} 
0

使用Geocoderクラスあなたの緯度/経度から住所を取得します。国は住所の一部でなければなりません。

注:アドレスがない場所(国、森など)では、この機能は動作しません。

更新:

(Androidが内部的に使用するのと同じ)Googleマップは、逆ジオコーディングをデータベースを使用しています素敵なサービスがあります:チャンスをうかがっhttp://reverse-geocoding.appspot.com/

は実際にも、中央の(任意の時点の国のデータを提供しますグリーンランドの) - あなたは大丈夫でしょう。

+0

次使用してみてください役立つかもしれない... uがへのリンクを持っていますチュートリアル、または任意のコード例? – Piyush

+0

よろしいですか? 'Geocode.get ..'は1つのコマンドです。あなたはそれを把握しています。 –

-1

私の答えバッグの3つの事:アンドロイドで

  1. 逆ジオコーディング:http://www.smnirven.com/?p=39
  2. Foursquares API
  3. は、独自のデータベースを維持します。

あなたにとって、最初のものはすばやく簡単です.3番目のものは最も信頼性が高くカスタムです。

0

コード

uは私を与えたのリンクを実装する方法
Geocoder gc = new Geocoder(this, Locale.getDefault()); 
    try { 
    List<Address> addresses = gc.getFromLocation(lat, lng, 1); 
    if (addresses.size() > 0) { 
     Address address = addresses.get(0); 

      String state = address.getLocality(); 
    } 

    } catch (IOException e) {} 
0
public static String getUserAddress(String lat, String lon) { 
     String userlocation = null; 
     String userCountry = null; 
     String userCountryCode = null; 
     String readUserFeed = readUserLocationFeed(lat.trim() + ","+ lon.trim()); 
     try { 
      JSONObject Strjson = new JSONObject(readUserFeed); 
      JSONArray jsonArray = new JSONArray(Strjson.getString("results")); 
      JSONArray array = new JSONArray(jsonArray.getJSONObject(0).getString("address_components")); 
      userCountry = array.getJSONObject(2).getString("long_name").toString(); 
      userCountryCode = array.getJSONObject(2).getString("short_name").toString(); 
      userlocation = jsonArray.getJSONObject(1).getString("formatted_address").toString(); 
      System.out.println("User Address : "+userlocation +"\nUser Country : "+userCountry+"\nUser CountryCode : "+ userCountryCode); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     Log.i("User Location ", userlocation); 
     return userlocation; 
    } 

    public static String readUserLocationFeed(String address) { 
     StringBuilder builder = new StringBuilder(); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+ address + "&sensor=false"); 
     try { 
      HttpResponse response = client.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { 
       HttpEntity entity = response.getEntity(); 
       InputStream content = entity.getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        builder.append(line); 
       } 
      } else { 
       Log.e(ReverseGeocode.class.toString(), "Failed to download file"); 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return builder.toString(); 
    } 
関連する問題