代码之家  ›  专栏  ›  技术社区  ›  Ali Habibzadeh

googlemaps有时不返回字符串的地理编码值

  •  0
  • Ali Habibzadeh  · 技术社区  · 16 年前

    我有以下代码: 它基本上查看HTML列表和地理编码,并标记每个项目。它十有八九是正确的,但有时我会在控制台中得到一个设置为show的错误。

    我什么都想不出来。任何想法都很感激。

    $(function () {
    
    var map = null;
    var geocoder = null;
    
    function initialize() {
        if (GBrowserIsCompatible()) {
            // Specifies that the element with the ID map is the container for the map
            map = new GMap2(document.getElementById("map"));
            // Sets an initial map positon (which mainly gets ignored after reading the adderesses list)
            map.setCenter(new GLatLng(37.4419, -122.1419), 13);
            // Instatiates the google Geocoder class
            geocoder = new GClientGeocoder();
            map.addControl(new GSmallMapControl());
            // Sets map zooming controls on the map
            map.enableScrollWheelZoom();
            // Allows the mouse wheel to control the map while on it
        }
    }
    
    
    function showAddress(address, linkHTML) {
        if (geocoder) {
            geocoder.getLatLng(address,
    
        function (point) {
            if (!point) {
                console.log('Geocoder did not return a location for ' + address);
            }
            else {
                map.setCenter(point, 8);
                var marker = new GMarker(point);
                map.addOverlay(marker);
                // Assigns the click event to each marker to open its balloon
                GEvent.addListener(marker, "click", function () {
                    marker.openInfoWindowHtml(linkHTML);
                });
    
            }
        }
    );
        }
    } // end of show address function
    
    initialize();
    
    // This iterates through the text of each address and tells the map
    // to show its location on the map. An internal error is thrown if
    // the location is not found.
    $.each($('.addresses li a'), function () {
        var addressAnchor = $(this);
        showAddress(addressAnchor.text(), $(this).parent().html());
    });
    });
    

    查看以下HTML:

    <ul class="addresses">
                    <li><a href="#">Central London</a></li>
                    <li><a href="#">London WC1</a></li>
                    <li><a href="#">London Shoreditch</a></li>
                    <li><a href="#">London EC1</a></li>
                    <li><a href="#">London EC2</a></li>
                    <li><a href="#">London EC3</a></li>
                    <li><a href="#">London EC4</a></li>
                </ul>
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   virtualize    16 年前

    也许这是地理编码器的查询限制,因为您经常要求api?
    只是猜测而已。我不止一次遇到这个问题。