获取给定经纬度的地理位置名称。

23
我正在开发一个应用程序,在其中获取我的Android设备的纬度、经度并将其提交到Web服务器上。
在Web应用程序中,我使用Google地图显示位置详细信息。
这里我有大量的纬度和经度值,我在JSP中循环它们并创建多个带有信息窗口的标记。
现在问题是,我需要在Google地图的信息窗口中显示特定纬度和经度的位置名称。
有人能帮我编写Google Maps脚本或者告诉我如何从Android手机自身的纬度和经度值中获取位置名称,以便我可以将其提交到我的Web服务器吗?
6个回答

53

我在这里提供一个函数,只需传入纬度和经度,即可获取与此纬度和经度相关的所有信息。

public void getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        Log.v("IGA", "Address" + add);
        // Toast.makeText(this, "Address=>" + add,
        // Toast.LENGTH_SHORT).show();

        // TennisAppActivity.showDialog(add);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

2
什么是GUIStatics,如何解决这个错误:--> GUIStatics无法解析为变量。 - Hitesh Dhamshaniya
他是对的,哎呀:/我们需要完整的代码和解释:( - Cyph3rCod3r
2
那些不理解这段代码的人,首先要获取自己的纬度和经度,然后将这些值传递给该方法,您就可以获得有关您位置的所有信息。这是一个链接,您可以在其中找到经度和纬度。 http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ 如果您不需要GUIStatistics,请注释掉它。我不需要它,所以我只删除并获取我的Placemark信息。 - Naveed Ahmad

7
Geocoder myLocation = new Geocoder(yourContext, Locale.getDefault());
List<Address> myList = myLocation.getFromLocation(Double.parseDouble(latitude),Double.parseDouble(longitude), 1);
Address address = (Address) myList.get(0);
String addressStr = "";
addressStr += address.getAddressLine(0) + ", ";
addressStr += address.getAddressLine(1) + ", ";
addressStr += address.getAddressLine(2);

1
在这段代码中,只需获取经纬度。LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double m=location.getLatitude(); double n=location.getLongitude();
- Mayur R. Amipara

5
StringBuilder _homeAddress = null;
        try{
            _homeAddress = new StringBuilder();
            Address address = null;
            List<Address> addresses = _coder.getFromLocation(_lat,_lon,1);
            for(int index=0; index<addresses.size(); ++index)
            {
                address = addresses.get(index);
                _homeAddress.append("Name: " + address.getLocality() + "\n");
                _homeAddress.append("Sub-Admin Ares: " + address.getSubAdminArea() + "\n");
                _homeAddress.append("Admin Area: " + address.getAdminArea() + "\n");
                _homeAddress.append("Country: " + address.getCountryName() + "\n");
                _homeAddress.append("Country Code: " + address.getCountryCode() + "\n");
                _homeAddress.append("Latitude: " + address.getLatitude() + "\n");
                _homeAddress.append("Longitude: " + address.getLongitude() + "\n\n");
            }
        }
        catch(Exception e){
            
        }

4

使用android.location.geocoder.getFromLocation(double latitude, double longitude, int maxResults)方法,可以得到地址列表。


3
   private String getAddress() {
        GPSTracker   gps = new GPSTracker(ProductList.this);
        Geocoder geocoder = new Geocoder(ProductList.this, Locale.getDefault());
        String address="";
        try {
            List<Address> addresses = geocoder.getFromLocation(gps.getLatitude(), gps.getLongitude(), 1);
            Address obj = addresses.get(0);
            String  add = obj.getAddressLine(0);
//optional
          /*  add = add + "\n" + obj.getCountryName();
            add = add + "\n" + obj.getCountryCode();
            add = add + "\n" + obj.getAdminArea();
            add = add + "\n" + obj.getPostalCode();
            add = add + "\n" + obj.getSubAdminArea();
            add = add + "\n" + obj.getLocality();
            add = add + "\n" + obj.getSubThoroughfare();*/

            Log.e("Location", "Address" + add);
            address=add;
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        return address;
    }

2
 public void getAddress(double lat, double lng) {
            Geocoder geocoder = new Geocoder(DistaceCalculating.this, Locale.getDefault());
            try {
                List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
                Address obj = addresses.get(0);
                String   add = obj.getAddressLine(0);
                String  currentAddress = obj.getSubAdminArea() + ","
                        + obj.getAdminArea();
                double   latitude = obj.getLatitude();
                double longitude = obj.getLongitude();
                String currentCity= obj.getSubAdminArea();
                String currentState= obj.getAdminArea();
               add = add + "\n" + obj.getCountryName();
                add = add + "\n" + obj.getCountryCode();
                add = add + "\n" + obj.getAdminArea();
                add = add + "\n" + obj.getPostalCode();
                add = add + "\n" + obj.getSubAdminArea();
                add = add + "\n" + obj.getLocality();
                add = add + "\n" + obj.getSubThoroughfare();
    
                
                System.out.println("obj.getCountryName()"+obj.getCountryName());
                System.out.println("obj.getCountryCode()"+obj.getCountryCode());
                System.out.println("obj.getAdminArea()"+obj.getAdminArea());
                System.out.println("obj.getPostalCode()"+obj.getPostalCode());
                System.out.println("obj.getSubAdminArea()"+obj.getSubAdminArea());
                System.out.println("obj.getLocality()"+obj.getLocality());
                System.out.println("obj.getSubThoroughfare()"+obj.getSubThoroughfare());
    
                
                Log.v("IGA", "Address" + add);
                // Toast.makeText(this, "Address=>" + add,
                // Toast.LENGTH_SHORT).show();
    
                // TennisAppActivity.showDialog(add);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接