如何在 HTML 5 中获取纬度或经度

发布于 2024-10-11 19:31:37 字数 442 浏览 2 评论 0原文

几个月前,我写了一些东西来从 google API 获取纬度或经度。后者我从 drupal 获取世界上大多数城市的纬度或经度数据库。

但问题是,同一个城市名称在一个地区可以出现两次或多次。就像印度和孟加拉国的菲罗扎巴德一样。北方邦的阿格拉(Agra)和拉贾斯坦邦的琼脂(agar)。

意味着用户名称混淆,如果他们发现两个同名的城市,他们就会混淆。

我听说 HTML 5 支持访问者的纬度或经度,但我需要他们出生的地方或他们想要用来填写表单的城市的纬度或经度。

我如何从 Google 等 API 获取纬度或经度。

过程是:

用户将位置放入文本框中以获取其纬度或经度。

为了正确的事情,我想向他们展示所有位置[如果找到多个相同的东西]。

他们可以选择正确的位置,单击后可以获得纬度和经度。

我如何使用任何 API 来做到这一点。

few month ago i write something to get the latitude or longitude from google API. latter i get the database from drupal for latitude or longitude to most of city in worlds.

but the problem is that the same city name can be found two or more times in a area. like Firozabad in India and bangladesh both. Agra in UP and agar in Rajasthan.

means the confusion in name by user if they found two city by same name they are confused.

i hear that HTML 5 support latitude or longitude of the visiter but i need latitude or longitude where they are born or city they want to use to fill a form.

how i can get the latitude or longtiude from API like google and some other.

the process is that:

user put the location in textbox for getting their latitude or longitude.

for right thing i want to show them all location [if same thing found more then one].

they can choose the right location and after click they can get the lati and langitude.

how i can do this using any API.

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

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

发布评论

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

评论(2

花开浅夏 2024-10-18 19:31:37

如果我理解正确的话,那么这里有一个 Javascript 函数,它根据用户输入的地址(完整或部分)返回实际位置列表:

getLocationsByAddress = function(address, onComplete) {
    var geocoder = null;
    var locations = [];
    var location = null;

    geocoder = new GClientGeocoder();
    geocoder.getLocations(address, function(response) {
        if (!response || response.Status.code != 200) {
            if (typeof (onComplete) != 'undefined') {
                onComplete([]);
            }
        } else {
            for (var i = 0; i < response.Placemark.length; i++) {
                location = response.Placemark[i];
                locations[locations.length] = {
                    address: location.address,
                    x: location.Point.coordinates[1],
                    y: location.Point.coordinates[0],
                    zoom: 14
                }
            }

            if (typeof (onComplete) != 'undefined') {
                onComplete(locations);
            }
        }
    });
}

该代码使用 API 版本 2,但我认为应该是使其与 V3 配合使用相当容易(请参阅此 API 参考 了解详情)。

更新

正如 @Maxym 所说,V2 API 已被视为已弃用,因此不应再使用。

看一下这个 jQuery 插件。它声称可以与 V2 和 V3 一起使用,因此您可能需要检查源代码以了解它们如何与 V3 一起使用。

If I understood you correctly then here's a Javascript function that returns a list of actual locations based on the address (full or partial) that user has entered:

getLocationsByAddress = function(address, onComplete) {
    var geocoder = null;
    var locations = [];
    var location = null;

    geocoder = new GClientGeocoder();
    geocoder.getLocations(address, function(response) {
        if (!response || response.Status.code != 200) {
            if (typeof (onComplete) != 'undefined') {
                onComplete([]);
            }
        } else {
            for (var i = 0; i < response.Placemark.length; i++) {
                location = response.Placemark[i];
                locations[locations.length] = {
                    address: location.address,
                    x: location.Point.coordinates[1],
                    y: location.Point.coordinates[0],
                    zoom: 14
                }
            }

            if (typeof (onComplete) != 'undefined') {
                onComplete(locations);
            }
        }
    });
}

The code uses API version 2 but I think it should be fairly easy to make it work with V3 (please see this API reference for details).

UPDATE

As @Maxym stated the V2 API is considered as deprecated and therefore shouldn't be used any further.

Take a look at this jQuery plugin. It claims to work with both V2 and V3 so you may want to examine the source code in order to find out how they do an it with V3.

乱了心跳 2024-10-18 19:31:37

我不确定它是否与 HTML 5 有关。您只需要创建某种提示,用户可以在其中选择她所指的确切城市(就像 Google 在maps.google.com 上所做的那样 - 输入“敖德萨”并查看列表具有此类名称的城市)。 Google 有 geocoder API 但可能它应该与 Google 地图一起使用(只需阅读许可证)。
所以这个过程很简单:用户输入城市,将其发送到地理编码器以获取具有该名称的城市列表及其坐标。然后您显示该列表,用户选择一个,您就得到了它的坐标。
文档介绍了如何在 JavaScript 中使用 Geocoding API v3,如下以及例子。 展示了如何从地址获取坐标,请采取考虑到它们仅处理结果[0],但您需要迭代结果才能获取所有城市。

I am not sure that it is about HTML 5. You just need to create sort of hints, where user can select which exactly city she means (the way Google does on maps.google.com - you enter "Odessa" and see list of cities with such name). Google has geocoder API but probably it should be used with Google Map (just read license).
So the process is easy: user enters city, you send it to geocoder to get list of cities with such a name and their coordinates. THen you show that list, user selects one and you have its coordinates.
There is documentation how to use Geocoding API v3 in JavaScript, as well as examples. This one show how to get coordinates from address, take into account that they process results[0] only, but you need to iterate over results to get all cities.

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