首页 > 解决方案 > 我需要帮助理解的Javascript google map api函数

问题描述

我主要做后端工作,对 JavaScript 知之甚少(强调一点)。我遇到了一些嵌入在网站 html 页面中的 javascript 代码。现在我不确定这里发生了什么。

我的理解是,它是某种 javascript google map api 函数(也许我错了)。我需要了解的是它在做什么。

<script type="text/javascript" src="http://maps.googleapis.com/maps/api    
    /js?key=*********************&libraries=places">
</script>
<script type="text/javascript">
    google.maps.event.addDomListener(window, 'load', function () {
        var places = new google.maps.places.Autocomplete(document.getElementById('txtPlaces'));
        google.maps.event.addListener(places, 'place_changed', function () {                                                                                                
            var place = places.getPlace();
            var address = place.formatted_address;
            var latitude = place.geometry.location.lat();
            var longitude = place.geometry.location.lng();
            var mesg = "Address: " + address;
           /* mesg += "\nLatitude: " + latitude;
            mesg += "\nLongitude: " + longitude;
            alert(mesg);*/
    document.getElementById('locc').value =   address;
        document.getElementById('latttt').value = latitude;
       document.getElementById('lonnnn').value = longitude;
        });
    });
</script>
<input type="hidden" name="loc" id="locc" value="">
<input type="hidden" name="latitu" id="latttt" value="">
<input type="hidden" name="longitu" id="lonnnn" value="">

标签: javascripthtml

解决方案


https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete

然后它根据来自谷歌地图的数据设置三个输入框的值。

(另外,在旁注中,我会在脚本标签中隐藏个人 api 密钥以防万一)


推荐阅读