从android中的坐标获取地理位置地址

时间:2012-10-13 09:46:57

标签: java android eclipse google-maps

android中,我可以很轻松地使onLocationChanged工作并获得移动经纬度。

然而,一旦我得到这些坐标,我如何获得移动设备的地址,例如

XYWZ Road, GRDSASDF Sector 823432, Australia  etc

5 个答案:

答案 0 :(得分:2)

地理编码器是用于处理地理编码反向地理编码的类。

  1. 地理编码是转换街道地址或其他地址的过程 将位置描述为(纬度,经度)坐标。
  2. 反向地理编码是转换a(纬度, 经度)坐标到(部分)地址。
  3. 反向地理编码位置描述中的详细信息量可能会有所不同,例如,一个可能包含最近建筑物的完整街道地址,而另一个可能只包含城市名称和邮政编码。 Geocoder类需要一个未包含在核心android框架中的后端服务。如果平台中没有后端服务,则Geocoder查询方法将返回空列表。使用isPresent()方法确定是否存在Geocoder实现。

    getFromLocation(double latitude, double longitude, int maxResults)方法返回一个地址数组,这些地址已知用于描述紧邻给定纬度和经度的区域。返回的地址将针对提供给此类构造函数的语言环境进行本地化。

    可以通过网络查找获得返回的值。结果是最佳猜测,并不保证有意义或正确。从与主UI线程分开的线程调用此方法可能很有用。

    Here熟悉Geocoder的教程:

    public class AndroidFromLocation extends Activity {
    
    double LATITUDE = 37.42233;
    double LONGITUDE = -122.083;
    
       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
           TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
           TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
           TextView myAddress = (TextView)findViewById(R.id.myaddress);
    
           myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
           myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));
    
           Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
    
           try {
      List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
    
      if(addresses != null) {
       Address returnedAddress = addresses.get(0);
       StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
       for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
       }
       myAddress.setText(strReturnedAddress.toString());
      }
      else{
       myAddress.setText("No Address returned!");
      }
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      myAddress.setText("Canont get Address!");
     }
    
       }
    }
    

    enter image description here

答案 1 :(得分:0)

您可以使用Android中的GeoCoder课程。

使用方法

List<Address>    getFromLocation(double latitude, double longitude, int maxResults)

答案 2 :(得分:0)

使用GeoCoder方法。

Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0) 
    System.out.println(addresses.get(0).getLocality());

答案 3 :(得分:0)

首先,你找到lat long,你说你已经完成了。

然后,您可以使用以下代码获取列表中的完整地址。这是代码:

// this will fetch the data of current address  
                    List<Address> addresses=geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10);

                    int i=1;
                    for(Address addObj:addresses)
                    {
                        // this will make the loop run for 1 time 
                        if(i==1)
                        {
                            //variables to split address line
                            String add_line1_extract;
                            //setting street address
                            streetaddressText.setText(addObj.getAddressLine(0));
                            //splitting city and state
                            add_line1_extract=addObj.getAddressLine(1);
                            String string = add_line1_extract;
                            String[] parts = string.split(",");
                            //Setting city
                            part1 = parts[0]; 
                            cityText.setText(part1);
                            //setting state
                            String part2 = parts[1]; 
                            stateText.setText(part2);
                            //setting country
                            countryText.setText(addObj.getAddressLine(2));

                            i++;

                            progress.setVisibility(View.GONE);
                        }
                    }

答案 4 :(得分:0)

试试这段代码:

public class LocationSpeecher extends MapActivity{
MapController mc;
       MapView myMapView;
       MapController mapController;
       GeoPoint point;
       MyPositionOverlay positionOverlay;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_speecher);

         MapView myMapView=(MapView)findViewById(R.id.myMapView);           
            mapController=myMapView.getController();   


           myMapView.displayZoomControls(true);        
           mapController.setZoom(17);



          // myMapView.setSatellite(true);

           myMapView.setStreetView(true);
           myMapView.setTraffic(true);              



            LocationManager locationManager; 
            String context = Context.LOCATION_SERVICE; 
            locationManager = (LocationManager)getSystemService(context); 

            Criteria crta = new Criteria(); 
            crta.setAccuracy(Criteria.ACCURACY_FINE); 
            crta.setAltitudeRequired(false); 
            crta.setBearingRequired(false); 
            crta.setCostAllowed(true); 
            crta.setPowerRequirement(Criteria.POWER_LOW); 
            String provider = locationManager.getBestProvider(crta, true); 

         // String provider = LocationManager.GPS_PROVIDER; 
            Location location = locationManager.getLastKnownLocation(provider); 
            updateWithNewLocation(location); 

            locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); 
            } 
        private final LocationListener locationListener = new LocationListener() 
        { 

        @Override 
        public void onLocationChanged(Location location) { 
        updateWithNewLocation(location); 
        } 

        @Override 
        public void onProviderDisabled(String provider) { 
        updateWithNewLocation(null); 
        } 

        @Override 
        public void onProviderEnabled(String provider) { 
        } 

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

        }; 

        private void updateWithNewLocation(Location location) { 
            String latLong;
            TextView myLocation; 
            myLocation = (TextView) findViewById(R.id.myLocation); 

            String addressString = "no address found"; 

            if(location!=null) {            



                Double geoLat=location.getLatitude()*1E6;
                Double geoLng=location.getLongitude()*1E6;
                GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());

                mapController.animateTo(point);

            double lat = location.getLatitude(); 
            double lon = location.getLongitude(); 
            latLong = "Lat:" + lat + "\nLong:" + lon;           

            double lattitude = location.getLatitude(); 
            double longitude = location.getLongitude(); 

            Geocoder gc = new Geocoder(this,Locale.getDefault()); 
            try { 
            List<Address> addresses= gc.getFromLocation(lattitude, longitude, 1); 
            StringBuilder sb = new StringBuilder(); 
            if(addresses.size()>0) { 
            Address address=addresses.get(0);
            for(int i=0;i<address.getMaxAddressLineIndex();i++)
                sb.append(address.getAddressLine(i)).append("\n");
            sb.append(address.getLocality()).append("\n"); 
            sb.append(address.getPostalCode()).append("\n"); 
        sb.append(address.getCountryName()); 
        } 
            addressString = sb.toString(); 
            } 
            catch (Exception e) { 
            } 
            } else { 
            latLong = " NO Location Found "; 
            } 

            myLocation.setText("Current Position is :\n"+ latLong + "\n"+  addressString ); 

}
相关问题