在页面加载时将地址传递给Google地图 [英] Passing address to Google Maps on page load
问题描述
使用地理编码初始化Google地图有一些问题。第一个问题是在$ gmap字符串中使用任何逗号,第二个问题是获取gmap_initialize未定义。
<?php $ gmap =布拉格,捷克共和国我知道一切以外的功能是正确的, ; ?>
< script type =text / javascript>
函数gmap_initialize(){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':<?php echo $ gmap;?>},function(results,status){
if(status == google.maps.GeocoderStatus.OK) {
var options = {
zoom:16,
position:results [0] .geometry.location,
center:results [0] .geometry.location,$ b $ (map.canElementById(map_canvas),options);
var marker = new google.maps.Marker({
map:map,
position:results [0] .geometry.location
});
} else {
警报(地理编码不成功,原因如下:+状态);
}
});
}
< / script>
您是否看过脚本的输出?
我的猜测是这样的:
geocoder.geocode({地址':捷克共和国布拉格},函数(结果,状态){/ / c $ c>
在你的PHP脚本中,
geocoder.geocode({'address':'<?php echo $ gmap;?>'},function(results,status ){/ code>
,您可能希望将单引号从 $ gmap
变量。
Having some problems initialising a google map using geocoding. First issue is with any commas being used in the $gmap string, second issue is with getting a "gmap_initialize is not defined". I know everything outside of the function is correct, any ideas?
<?php $gmap = "Prague, Czech Republic"; ?>
<script type="text/javascript">
function gmap_initialize() {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': <?php echo $gmap; ?>}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var options = {
zoom: 16,
position: results[0].geometry.location,
center: results[0].geometry.location,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), options);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
Did you look at the output of your script?!
My guess is it looks something like:
geocoder.geocode( { 'address': Prague, Czech Republic}, function(results, status) {
In your PHP script, you probably actually want something like:
geocoder.geocode( { 'address': '<?php echo $gmap; ?>'}, function(results, status) {
and you'll probably want to escape single quotes out of the $gmap
variable.
这篇关于在页面加载时将地址传递给Google地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!