【问题标题】:Is Geocoder Still Working For Javascript?地理编码器仍然适用于 Javascript 吗?
【发布时间】:2013-05-27 06:24:53
【问题描述】:

您好,我想知道 Geocoder 是否仍在工作?当我初始化新的 google.maps.geocoder(); 时,它似乎没有反应。我这里的目的是从经纬度中获取位置地址。

警报 A 和 B 正在工作。停在警报 C。

$('#btn').click(function initialize() {
            alert("A");
            var loc = {};
            alert("B");
            var geocoder = new google.maps.Geocoder();
            alert("C");
            if(google.loader.ClientLocation) {
                alert("D");
                loc.lat = google.loader.ClientLocation.latitude;
                loc.lng = google.loader.ClientLocation.longitude;

                var latlng = new google.maps.LatLng(loc.lat, loc.lng);
                geocoder.geocode({'latLng': latlng}, function(results, status) {
                    if(status == google.maps.GeocoderStatus.OK) {
                        alert(results[0]['formatted_address']);
                    };
                });
            }
        });

【问题讨论】:

  • alert("C"); 之后使用alert(google.loader.ClientLocation); 并说出你得到了什么。用google.loader.ClientLocation解释你,在调用这个函数之前你有纬度和经度吗?
  • 返回空值。为什么会这样?我已经包含了 jsapis.js 文件。
  • @Bryan Learn 我的回答可以从 Lat,Lng 获取地址吗?
  • @Lashmana Kumar,似乎无法加载 ClientLocation

标签: javascript jquery google-maps


【解决方案1】:

试试这个从 lat、lng 获取地址。单击标记以显示地址。它是完成您的工作的另一种方式。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Testing</title>
    <script src="scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA7IZt-36CgqSGDFK8pChUdQXFyKIhpMBY&sensor=true" type="text/javascript"></script>
    <script type="text/javascript">

        var map;
        var geocoder;
        var marker;
        var latlng;
        var infowindow;

        $(document).ready(function() {

        });

        function initialize() {
            var mapOptions = {
                //center: new google.maps.LatLng(40.4230, 98.7372),   // Coimbatore = (11.0168445, 76.9558321)
                zoom: 6,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

            geocoder = new google.maps.Geocoder();
            infowindow = new google.maps.InfoWindow();

            var latlngStr = $('#address').val().split(",");
            var lat = parseFloat(latlngStr[0]);
            var lng = parseFloat(latlngStr[1]);

            latlng = new google.maps.LatLng(lat, lng);
            marker = new google.maps.Marker({
                position: latlng,
                map: map,
                draggable: true
            });
            map.setCenter(latlng);

            google.maps.event.addListener(marker, 'click', function(event) {
                geocoder.geocode({ 'latLng': event.latLng }, function(results, status) {
                    infowindow.setContent(results[0].formatted_address);
                });

                infowindow.setContent(this.html);
                infowindow.setPosition(event.latLng);
                infowindow.open(map, this);
            });
        }

    </script>

</head>
<body>
    <label>
        Lat, Lng:
    </label>
    <input id="address" type="text" placeholder="10.9435131, 76.9383790" />
    <input type="button" value="Go" onclick="initialize()" />
    <br />
    <br />
    <div id="map-canvas" style="width: 800px; height: 500px">
    </div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-16
    • 2014-01-30
    • 2013-09-26
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多