3

我正在使用谷歌地图 API 3 和谷歌地理编码器。问题是它没有正确加载地图我通过 ajax 带来数据并调用一个函数showAddress(elemId, address) where elementId 是 div id 将在其中呈现地图。这是谷歌地图的代码

var geocoder;
var map;

    // run every mouse over
function init_gmap(elemId) {
    var mapOptions = {
        zoom: 6
    };
    map = new google.maps.Map(document.getElementById(elemId),
        mapOptions)
}

    // find the address
function showAddress(elemId, address) {
        // todo: some caching?
        init_gmap(elemId); // need to call this every time cause we're showing a new map

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

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

                    google.maps.event.trigger(map, 'resize')


                } 
                else 
                {

                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
}

有什么建议吗?

谢谢你。

更新代码:

<script type="text/javascript">
//<![CDATA[

var geocoder;
var map;
var lat;
var lng;
var marker;

    // run every mouse over
function init_gmap(elemId,address) {
    geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status){
//        console.log(results[0].geometry.location.YA);
        lat = results[0].geometry.location.Ya;
        lng = results[0].geometry.location.Za;
            var mapOptions = {
                zoom: 15,
                center:  new google.maps.LatLng(lat, lng),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById(elemId),
                mapOptions)
    });

}

    // find the address
function showAddress(elemId, address) {
        // todo: some caching?
        init_gmap(elemId,address); // need to call this every time cause we're showing a new map
        marker = "marker_"+elemId;
        marker = new google.maps.Marker({
            map: map, 
            position:  new google.maps.LatLng(lat, lng)
        });

        var infowindow = new google.maps.InfoWindow({
        map: map,
        position:  new google.maps.LatLng(lat, lng),
        content: address
        });

}
</script>
4

1 回答 1

2

为了初始化地图,您需要将 center 和 mapTypeId 添加到 mapOptions(所有这些都是必需的,如文档中所述)。例如:

    var mapOptions = {
        center:  new google.maps.LatLng(40.178873, -96.767578),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        zoom: 6
}
于 2012-08-04T09:57:10.303 回答