在页面加载时将地址传递给 Google 地图

发布于 2024-10-02 03:11:14 字数 921 浏览 4 评论 0原文

使用地理编码初始化谷歌地图时遇到一些问题。第一个问题是 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归途 2024-10-09 03:11:14

你看过你的脚本的输出吗?!

我的猜测是它看起来像:

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.

删除→记忆 2024-10-09 03:11:14

这看起来像是 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?

蝶…霜飞 2024-10-09 03:11:14

通常,当页面上的其他内容不太正确时(例如缺少一个关闭标记),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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文