我使用的是谷歌地图API 3和谷歌地理编码器。问题是它没有显示标记和信息窗口。我通过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
})
- 您没有检查对地理编码器的调用是否成功(地址是什么?)
-
如果你没有使用文档化的界面,这些将随着发布而改变:
lat = results[0].geometry.location.Ya; lng = results[0].geometry.location.Za;