对一组坐标进行反向地理编码

发布于 2024-08-28 12:11:06 字数 733 浏览 6 评论 0原文

我有以下格式的坐标集

(-33.9,18.6)

我如何获取这些坐标最近的城镇或国家/地区的名称?我猜它会涉及 Javascript,但如果合适的话我很高兴也使用 PHP?

编辑:我正在尝试 Google 反向地理编码器,但遇到问题它。下面的代码与他们的示例之一非常相同,但似乎根本没有运行......有什么想法吗?

<script type="text/javascript" charset="utf-8">
            function reverseGeocode(lat,lon){

                var geocoder = new GClientGeocoder();
                var latlng = new GLatLng(lat, lon);
                geocoder.getLocations(latlng, function(addresses) {
                    alert(addresses);
                });

            }
        </script>

I have sets of co-ordinates in the following format

(-33.9,18.6)

How do I go about getting the name of the nearest town or Country for those co-ords? I'm guessing it will involve Javascript, but am happy to also use PHP if appropriate?

EDIT: Am trying the Google Reverse Geocoder but having trouble with it. The following code is pretty identical to one of their examples but doesn't seem to be running at all... any ideas why?

<script type="text/javascript" charset="utf-8">
            function reverseGeocode(lat,lon){

                var geocoder = new GClientGeocoder();
                var latlng = new GLatLng(lat, lon);
                geocoder.getLocations(latlng, function(addresses) {
                    alert(addresses);
                });

            }
        </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

调妓 2024-09-04 12:11:06

google 有一个网络服务,您可以调用它来进行(反向)地理编码,它比使用完整的 javascript api 轻量得多。

v2 的示例:http://maps.google.com/maps/geo?q=51,4&sensor=false&output=json&key=insert_your_api_key_here&callback=parseme

并使用 v3:
http://maps.google.com google.com/maps/api/geocode/json?latlng=51,4&sensor=false&callback=parseme

在这两种情况下,您都会得到 json 响应(javascript 对象),区别在于现在已弃用v2 你需要提供你的 api 密钥,你可以使用回调(为了跨站点 ajax 的优点),它可以大声喊出来 v3 不再支持(但您不再需要 api 密钥)。

google has a webservice you can call to do (reverse) geocoding, which is much more lightweight then using the full javascript api.

an example with v2: http://maps.google.com/maps/geo?q=51,4&sensor=false&output=json&key=insert_your_api_key_here&callback=parseme

and with v3:
http://maps.google.com/maps/api/geocode/json?latlng=51,4&sensor=false&callback=parseme

in both cases you get a json response (javascript object), the difference being that in the now deprecated v2 you need to provide you api key and you can use a callback (for cross site ajax goodness) which for crying out loud isn't supported in v3 any more (but you don't need an api key any more).

壹場煙雨 2024-09-04 12:11:06

您注册了他们的 API 密钥吗?

还有具体的错误信息吗?

Have you registered for their API key?

Also are there any specific error messages?

橙幽之幻 2024-09-04 12:11:06

好的,成功让它工作了,所以这就是我所做的:

<script src="http://maps.google.com/maps?file=api&v=2&key=INSERTAPIKEYHERE" type="text/javascript"></script>
function reverseGeocode(latitude,longitude){

            var geocoder = new GClientGeocoder();
            var latlng = new GLatLng(latitude, longitude);

            geocoder.getLocations(latlng, function(addresses) {
                var address = addresses.Placemark[0].address;
                var country = addresses.Placemark[0].AddressDetails.Country.CountryName;
                var locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;

                alert(address);
            });
        }

Ok managed to get it working, so here's what I did:

<script src="http://maps.google.com/maps?file=api&v=2&key=INSERTAPIKEYHERE" type="text/javascript"></script>
function reverseGeocode(latitude,longitude){

            var geocoder = new GClientGeocoder();
            var latlng = new GLatLng(latitude, longitude);

            geocoder.getLocations(latlng, function(addresses) {
                var address = addresses.Placemark[0].address;
                var country = addresses.Placemark[0].AddressDetails.Country.CountryName;
                var locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;

                alert(address);
            });
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文