在页面加载时将地址传递给 Google 地图
使用地理编码初始化谷歌地图时遇到一些问题。第一个问题是 $gmap 字符串中使用的任何逗号,第二个问题是获取“gmap_initialize 未定义”。我知道函数之外的所有内容都是正确的,有什么想法吗?
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你看过你的脚本的输出吗?!
我的猜测是它看起来像:
geocoder.geocode( { 'address': 布拉格,捷克共和国}, function(results, status) {
在你的 PHP 脚本中,你可能实际上想要这样的东西:
< code>geocoder.geocode( { 'address': ''}, function(results, status) {
并且您可能希望将单引号从
$gmap
变量。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.这看起来像是 JavaScript 错误。说这个函数(看起来正确)没有定义。也许对 gmap_initialize 的调用发生在这个定义之前?
This looks like a JavaScript Error. Saying that this function (which looks right) is not defined. Maybe the call to gmap_initialize happens before this definition?
通常,当页面上的其他内容不太正确时(例如缺少一个关闭标记),JavaScript 会出现此错误...
您是否已使用 W3C 验证了代码?
Usually this error comes with JavaScript when something else on the page is not quite right, such as a single missing close tag...
Have you validated the code with W3C?