2013-03-21 1 views
3

내가 서있는 지역의 세부 정보를 얻고 싶습니다. LocationManager 클래스를 사용하여 위도와 경도를 알고 있지만 이름과 같은 세부 정보를 얻는 방법은 무엇입니까? 지역, 주소 등? 제한된 수의 요청으로 인해 지오 코더를 사용하고 싶지 않습니다. Google Places API를 사용하여 해당 지역의 세부 정보를 찾는 방법은 무엇입니까? 당신은 여전히 ​​당신에 문제가있는 경우android의 Google Places API를 사용하여 도시 이름 및 지역 세부 정보 찾기

+0

이 링크는 도움이 될 수 있습니다 : http://stackoverflow.com/questions/2296377/how-to-get-city-name-from-latitude-and-longitude-coordinates-in-google-maps – yasin

답변

4

사용이 클래스는 경도와 위도를 검색 할 수

public class GPSTracker implements LocationListener { 

    private final Context mContext; 

    // flag for GPS status 
    boolean isGPSEnabled = false; 

    // flag for network status 
    boolean isNetworkEnabled = false; 

    // flag for GPS status 
    boolean canGetLocation = false; 

    Location location; // location 
    double latitude; // latitude 
    double longitude; // longitude 

    // The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

    // Declaring a Location Manager 
    protected LocationManager locationManager; 

    public GPSTracker(Context context) { 
     this.mContext = context; 
     getLocation(); 
    } 

    public Location getLocation() { 
     try { 
      locationManager = (LocationManager) mContext 
        .getSystemService(LOCATION_SERVICE); 

      // getting GPS status 
      isGPSEnabled = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 
      } else { 
       this.canGetLocation = true; 
       // First get location from Network Provider 
       if (isNetworkEnabled) { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

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

     return location; 
    } 

    /** 
    * Stop using GPS listener 
    * Calling this function will stop using GPS in your app 
    * */ 
    public void stopUsingGPS(){ 
     if(locationManager != null){ 
      locationManager.removeUpdates(GPSTracker.this); 
     } 
    } 

    /** 
    * Function to get latitude 
    * */ 
    public double getLatitude(){ 
     if(location != null){ 
      latitude = location.getLatitude(); 
     } 

     // return latitude 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    * */ 
    public double getLongitude(){ 
     if(location != null){ 
      longitude = location.getLongitude(); 
     } 

     // return longitude 
     return longitude; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    * @return boolean 
    * */ 
    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 


    public void onLocationChanged(Location location) { 
    viewManager(location); 
    } 


    public void onProviderDisabled(String provider) { 
    } 


    public void onProviderEnabled(String provider) { 
    } 


    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 


} 

세부 사항 당신은

private void viewManager(Location loc) { 

     Toast.makeText(getBaseContext(), 
      "Location changed : Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), 
      Toast.LENGTH_SHORT).show(); 
     String longitude = "Longitude: " + loc.getLongitude(); 
     Log.v(TAG, longitude); 
     String latitude = "Latitude: " + loc.getLatitude(); 
     Log.v(TAG, latitude); 

     /*----------to get City-Name from coordinates ------------- */ 
     StringBuffer address = new StringBuffer(); 
     Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); 
     List<Address> addresses; 
     try { 
     addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); 

     if (addresses.size() > 0) 
      System.out.println(addresses.get(0).getLocality()); 
     address.append(addresses.get(0).getAddressLine(0)).append("\n") 
       .append(addresses.get(0).getAddressLine(1)).append("\n") 
       .append(addresses.get(0).getAddressLine(2)); 

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

     String s = longitude + "\n" + latitude + "\n\nMy Currrent City is: \n" + address.toString(); 

    } 
0

스토어 getFromLocation 주소 객체 (더블, 더블, int) 메소드의 결과를 해결할 수 있다고 생각합니다().