谷歌地图api 3 ajax电话

时间:2012-08-04 09:33:35

标签: ajax codeigniter google-maps jquery google-maps-api-3

我正在使用谷歌地图API 3和谷歌地理编码器。问题是它没有正确加载地图我通过ajax引入数据并调用函数showAddress(elemId, address) 其中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>

1 个答案:

答案 0 :(得分:2)

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

    var mapOptions = {
        center:  new google.maps.LatLng(40.178873, -96.767578),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        zoom: 6
}