如何在Google Map API中使用Address而不是Lat / Long [英] How to use Address instead of Lat/Long in Google Map API

查看:153
本文介绍了如何在Google Map API中使用Address而不是Lat / Long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Google地图标记来标记我们提供服务的地点。
我的代码使用纬度/长时间工作正常,但在我的项目中,我需要使用城市名称而不是经纬度。我怎样才能做到这一点有没有办法做到这一点?
这里是我的代码

I want to use Google Map Markers to mark the places where we provide our services. MY Code using Lat/Long is working Fine but in my Project i need to use City Names instead of lat/long. How can i accomplish this is there any way to do this? here is my code

        <div id="map" style="height:500px;"></div>
    <script>
        var neighborhoods = [
          { lat: 31.6400, lng: 74.8600 },
          { lat: 28.6139, lng: 77.2090 },
          { lat: 12.9667, lng: 77.5667 },
          { lat: 31.3260, lng: 75.5760 },
          { lat: 30.7500, lng: 76.7800 },
          { lat: 30.3400, lng: 76.3800 }
        ];

        var markers = [];
        var map;

        function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
                zoom: 5,
                center: { lat: 28.6139, lng: 77.2090 }
            });
        }

        function drop() {
            clearMarkers();
            for (var i = 0; i < neighborhoods.length; i++) {
                addMarkerWithTimeout(neighborhoods[i], i * 200);
            }
        }

        function addMarkerWithTimeout(position, timeout) {
            window.setTimeout(function () {
                markers.push(new google.maps.Marker({
                    position: position,
                    map: map,
                    title: 'Jassie',
                    animation: google.maps.Animation.DROP
                }));
            }, timeout);
        }

        function clearMarkers() {
            for (var i = 0; i < markers.length; i++) {
                markers[i].setMap(null);
            }
            markers = [];
        }
        $(window).load(function () {
            drop();
        });
    </script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=API_Key&signed_in=true&callback=initMap">  </script>
</div>


推荐答案

您必须使用Geocoding API。以Lat / lng作为地址的示例:

You have to use the Geocoding API. An example to make an Address to Lat/lng:

 doGeocode: function (address, postal_code, callback) {
    console.log("TEST: "+address.toString());
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': address,
        'componentRestrictions': {
            'postalCode': postal_code,
            'country': 'de'
        }
    }, function (results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            callback(results);
        } else {
            //Error handling
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });

注意:地址也可以包含h​​ouseNumber,但输入一个空格。

Note: The address can also contain the houseNumber but enter a whitespace.

从Lat / lng获取地址也有相反的方法。以防万一需要:

There is also a reverse Way to get Addresses from Lat/lng. Just in case some needs that:

reverseGeocoder: function (lat, lng, callback) {

    var geocoder = new google.maps.Geocoder;
    var addressComponents = {};
    geocoder.geocode({
        'location': {
            lat:lat,
            lng: lng
        }
    }, function (results, status){
        if(status === google.maps.GeocoderStatus.OK){
            if (results[1]) {
                var result = results[0].address_components;

                for (var i = 0; i < result.length; i++) {
                    if (result[i].types[0] == "route") {
                        console.log(result[i].long_name);
                        addressComponents.route = result[i].long_name
                    }
                    if (result[i].types[0] == "street_number") {
                        console.log(result[i].long_name);
                        addressComponents.street_number = result[i].long_name
                    }
                    if (result[i].types[0] == "locality") {
                        console.log(result[i].long_name);
                        addressComponents.locality = result[i].long_name
                    }
                    if (result[i].types[0] == "postal_code") {
                        console.log(result[i].long_name);
                        addressComponents.postal_code = result[i].long_name
                    }
                }
                callback(addressComponents);
            } else {
                alert('No information found.');
            }

        }else {
            alert("Die geolocation abfrage konnte leider nicht ausgeführt werden. "+status);
        }
    });

从上面的第一个函数中,您将获得Lat / lng。
然后简单地使用这样的函数:

From the first function above you will get the Lat/lng. Then simply use a function like this:

var newMarkers = [];        //Global marker array
 marker: function (lat, lng) {
    console.log("new marker");

    //console.log("LAT: "+lat+ "LNG: "+lng);
    var marker = new google.maps.Marker({
        position: {lat: lat, lng: lng},
        icon: icon,
        map: map
    });
    markers.push(marker);
    marker.addListener('click', function () {
        //InfoWindow or whatever u want
    });

    map.setCenter({lat: lat, lng: lng});
    map.setZoom(16);
},

这篇关于如何在Google Map API中使用Address而不是Lat / Long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆