给定纬度和经度,获取位置名称

时间:2011-05-30 05:04:28

标签: android google-maps gps

我正在开发一个应用程序,我在Android设备中获取纬度,经度并将它们发布到网络服务器。

在网络应用程序中,我借助谷歌地图显示位置详细信息。

这里我有大量的lat long值,我将它循环到我的jsp中,并使用信息窗口创建多个标记。

现在问题是我需要在谷歌地图的信息窗口中显示特定纬度和经度的位置名称。

任何人都可以帮我使用谷歌地图脚本,或者如何从我的Android手机本身的lat long值中获取位置名称,以便我可以将其发布到我的网络服务器。

6 个答案:

答案 0 :(得分:34)

在这里我给了一个只是通过这个函数的纬度和经度然后你得到了与这个纬度和经度相关的所有信息。

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();
    }
}

我希望你能回答。

答案 1 :(得分: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);

答案 2 :(得分: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){

        }

答案 3 :(得分:3)

使用android.location.geocoder.getFromLocation(double latitude, double longitude, int maxResults)

它会给你一个地址列表。

答案 4 :(得分:1)

你去吧!

var user = firebase.auth().currentUser;
var uid = user.uid;
var hashKey = null;

firebase.database().ref('/userlist/' + uid + '/' + 'hashKey').once('value').then(function(snapshot) {
    hashKey = (snapshot.val());
    console.log(hashKey)
});

... snip ...

    body: '<a href="https://nudget-72ee4.firebaseapp.com/?hashkey='+ ((hashKey == null) ? 'no-key' : hashKey) +'">Join my grocery list!</a>',

... snip ...

答案 5 :(得分:0)

它为我工作:

 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();
            }
        }