谷歌地图API没有;t显示地图,如果中心由javascript函数填充
Google Maps API doesn't display map if the center is populated by a javascript function
所以,当我这样做时:
<!-- javascript file -->
function geocodeFromAdress(address) {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location;
console.log(latLng.lat() + ", " + latLng.lng());
return latLng.lat() + ", " + latLng.lng();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
<!-- HTML view -->
<script type="text/javascript">
$(function(){
myOptions = {
center: new google.maps.LatLng(geocodeFromAdress('Los Angeles')),
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
Gmaps.map = new google.maps.Map($('.map')[0], myOptions);
addFacilities(Gmaps.map);
});
</script>
地图没有被渲染,但如果我渲染了:
<script type="text/javascript">
$(function(){
myOptions = {
center: new google.maps.LatLng(34.0522342, -118.2436849),
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
Gmaps.map = new google.maps.Map($('.map')[0], myOptions);
addFacilities(Gmaps.map);
});
</script>
则映射被正确地渲染。为什么它不接受JS函数的协调?
JS函数的输出:34.0522342, -118.2436849
提前谢谢。
映射不会重新定义信标,因为mapOptions
中的center
需要一个值,但当您这样做时:
center: new google.maps.LatLng(geocodeFromAdress('Los Angeles'))
从geocodeFromAdress('Los Angeles')
返回的是undefined
,因此没有值传递给google.maps.LatLng
函数。
您可以临时给出一个中心,然后在地理编码器返回时设置中心:
function geocodeFromAdress(address) {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location;
console.log(latLng.lat() + ", " + latLng.lng());
Gmaps.map.setCenter(latLng);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
或者在地理编码器返回时初始化地图:
function geocodeFromAdress(address) {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location;
console.log(latLng.lat() + ", " + latLng.lng());
myOptions = {
center: new google.maps.LatLng(latLng),
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
Gmaps.map = new google.maps.Map($('.map')[0], myOptions);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
相关文章:
- 椭圆字幕选择,然后在Photoshop中使用Javascript填充颜色
- 使用javascript填充选择框
- 编辑后无法用Javascript填充文本框
- 用PHP/MMySQL和JavaScript填充几个HTML分区
- HTML5使用Javascript填充文本标记的有效方式
- 使用javascript填充Codeigniter(表单助手)-下拉列表
- 在 asp.net 中用javascript填充国家和城市下拉列表
- 创建唯一选项,然后使用 JavaScript 填充多个选择
- 如何使用Javascript填充输入文本字段
- 使用javascript填充棋盘棋子
- 如何使用由Javascript填充的Python从网站获取数据
- 如何使用行下方的JSON和Javascript填充表
- 停止jQuery表单验证重置由其他JavaScript填充的typeahead.js输入字段
- 用用户输入的javascript填充
- 用javascript填充对象数组
- 当用户输入时打开一个弹出窗口,使用javascript填充表中的输入字段
- 用Javascript填充剑道网格列菜单过滤选项
- 基于var创建JavaScript填充href
- Javascript填充缺失的范围(整数)
- Jquery 移动干扰 JavaScript 填充选择框