开发者

how to get the latitude or longitude in HTML 5

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 proble开发者_Python百科m 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜