2011-08-02 16 views
0

緯度と経度を使用して場所(例:地域、国、郵便番号)のGoogleマップを取得しようとしています。以下がnullのデータを得た私のコ​​ードです:住所を使用してGoogleマップから場所を取得できません

編集:

public void onLocationChanged(Location location) { 

    geocoder = new Geocoder(this, Locale.getDefault()); 

    if (location != null) { 

     try { 
      addresses = geocoder.getFromLocation(location.getLatitude()/1E6, 
        location.getLongitude()/1E6, 1); 
      if (addresses.size() > 0) { 
       resultAddress = addresses.get(0); 

       locality = resultAddress.getLocality(); 
       sublocality = resultAddress.getSubLocality(); 
       postalcode = resultAddress.getPostalCode(); 
       country = resultAddress.getCountryName(); 
       adminarea = resultAddress.getSubAdminArea(); 

      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

} 

私は手動で緯度と経度を割り当てようとしましたが、何も得ませんでした。

私の間違いを訂正する人はいますか?ありがとうございます

+0

これはエラーですか? – Finuka

答えて

4

私はこの問題がこのメソッドのパラメータにあると思います。

は編集

addresses = geocoder.getFromLocation(location.getLatitude()/1E6, 
        location.getLongitude()/1E6, 1); 

geopointの座標は、マイクロ度で表現されているので、私はこれを試してみてください場所ではなく

addresses = geocoder.getFromLocation(location.getLatitude(), 
        location.getLongitude(), 1); 

で、その場合にはGeoPointであることを推測します コードをコピーして試しました。あなたの「場所」オブジェクトを想定すると、次のようにGeoPoint、動作するコードがあるさ:

GeoPoint location = new GeoPoint(lat, lon); 
    if (location != null) { 
     Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
     try { 
      List<Address> addresses = geocoder.getFromLocation(location.getLatitudeE6()/1E6, 
        location.getLongitudeE6()/1E6, 1); 
      if (addresses.size() > 0) { 
       Address resultAddress = addresses.get(0); 

       String locality = resultAddress.getLocality(); 
       String sublocality = resultAddress.getSubLocality(); 
       String postalcode = resultAddress.getPostalCode(); 
       String country = resultAddress.getCountryName(); 
       String adminarea = resultAddress.getSubAdminArea(); 

      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

してますが、エミュレータ上でそれをテストしている場合は、あなたがインターネットに接続していることを確認してください。それ以外の場合、メソッド "getFromLocation"はアドレスを見つけることができず、何も表示されません。 logcatにエラーがなく、何も表示されないだけの問題がある場合、それは問題です。ネットワークなし

+0

まだ結果はnullです – Yoo

+0

あなたのロケーションオブジェクトのコードを投稿できますか?また、オブジェクトがnullでないことを確認していますか? – Finuka

+0

@Finuka私は、(lat、lng、place)をtextviewで表示すると、位置オブジェクトがnullではないと確信しています。テキストビューには緯度と経度が表示され、場所の情報だけがヌルです。 – Yoo

0

manifest.xmlに<uses-permission android:name="android.permission.INTERNET" />がありますか?

+0

はい、私はそれを試してみましたが、 – Yoo

関連する問題