微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP-Google Map API 3标记未显示

我正在使用Google Map API 3和Google Geocoder.问题是它没有显示标记和信息窗口,而是通过ajax带来了数据,并调用函数showAddress(elemId,address),其中elementId是div ID,将在其中渲染地图.这是谷歌地图的代码

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

var geocoder;
var map;
var lat;
var lng;
function showAddress(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)
            $('a#full-'+elemId).attr('href','http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q='+lat+','+lng+'')
            var marker 
            marker = "marker_"+elemId;
            myLatlng = new google.maps.LatLng(lat,lng);
            marker = new google.maps.Marker({
                id:elemId,
                position: myLatlng,
                map: map
            });
            var infowindow = "infowindow"+elemId;
            infowindow  = new google.maps.InfoWindow({
            content: 'Hello world'
            });
            infowindow.open(map, marker);
            google.maps.event.trigger(map, 'resize');
    });

}
</script>

解决方法:

首先.Ya和.Za不是文档化的属性,因此,如果您按如下方式使用它们

lat = results[0].geometry.location.Ya;
lng = results[0].geometry.location.Za;

代码可能会中断.

其次,results [0] .geometry.location已经是google.maps.LatLng()对象,因此无需分别提取lat和lng并创建一个新对象.您可以像这样使用它:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐