谷歌地图API3没有返回地图?

时间:2015-08-22 07:59:50

标签: google-maps google-maps-api-3

我试图在谷歌地图上制作基于地址的标记,但是地图显示没有数据?

这是我的例子

***Service

HTML

$(document).ready(function() {

    /* google maps */

    google.maps.visualRefresh = true;
    var geocoder = new google.maps.Geocoder();
    var mapOptions = {
        zoom: 14, // set the zoom level manually
        zoomControl: false,
        scaleControl: false,
        scrollwheel: false,
        disableDoubleClickZoom: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };


    var map;

    function initialize() {
        var address = $('#map').text(); /* change the map-input to your address */
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        if (geocoder) {
            geocoder.geocode({
                'address': address
            }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                        map.setCenter(results[0].geometry.location);

                        var infowindow = new google.maps.InfoWindow({
                            content: address,
                            map: map,
                            position: results[0].geometry.location,
                        });

                        var marker = new google.maps.Marker({
                            position: results[0].geometry.location,
                            map: map,
                            title: address
                        });

                    } else {
                        alert("No results found");
                    }
                }
            });
        }
    }
    google.maps.event.addDomListener(window, 'load', initialize);


});

1 个答案:

答案 0 :(得分:1)

center您错过了mapOptions。 您应该在geocoder函数中创建initialize变量。 请参考this。我已经展示了你的地图。