2011-08-02 8 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

나는이 문제가이 방법의 매개 변수에 있다고 생각한다. 나는 위치는 대신,이 경우에서는 GeoPoint의이라고 생각

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

노력이 :

addresses = geocoder.getFromLocation(location.getLatitude()/1E6, 
        location.getLongitude()/1E6, 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 위치 객체가 null이 아니라고 확신합니다. (lat, lng, place)를 textview를 통해 보여 주면됩니다. textview는 위도와 경도를 표시하며 장소 정보 만 null입니다. – Yoo

0

manifest.xml에 <uses-permission android:name="android.permission.INTERNET" />이 있습니까?

+0

네, 그랬어요 – Yoo

관련 문제