javascript - 简单的谷歌地图与地理编码

标签 javascript google-maps-api-3

我做了一个简单的 GoogleMap,它响应 script.php?q=canada 并显示 map 。

一个问题是它总是以中心 var latlng = new google.maps.LatLng(34.052234,-118.243685); 加载 map 一秒钟,然后加载正确的 map 。

我的代码有什么问题吗?

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title><?=$_GET['q']?></title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
            <script type="text/javascript">
              var geocoder;
              var map;
              function initialize() {
                geocoder = new google.maps.Geocoder();
                var latlng = new google.maps.LatLng(34.052234,-118.243685);
                var address = '<?=$_GET['q']?>';

                var myOptions = {
                  zoom: 10,
                  center: latlng,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                }
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                geocoder.geocode( { 'address': address}, function(results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {
                  map.setCenter(results[0].geometry.location);
                  // autozoom
                  map.fitBounds(results[0].geometry.viewport);
                    var marker = new google.maps.Marker({
                        map: map, 
                        position: results[0].geometry.location,
                        title: 'by Quick Maps',
                        clickable: false
                    });
                  } else {
                    alert("Geocode was not successful for the following reason: " + status);
                  }
                });
              }
            </script>
 </head>
<body onload="initialize()">
<div id="map_canvas"></div>
</body>
</html>

附带问题: 如何用 map 上的简单消息替换 alert("Geocode was not success for the following Reason: "+ status);

最佳答案

1) 谷歌地图初始化到一个位置,然后你告诉它另一个位置。所以你有两个选择:

a)Move the map instantiation into the geocode callback.
b)Initialize to the location you want.  To do this you will need to read a bit more about how google maps works instead of just using their basic example.

2) 至于好的错误消息而不是粗俗的警报,请将其添加到您的 html 中。

<p id="errorMessage" style="display:none;">Sorry, couldn't find that address</p>

然后显示它而不是警报。

} else {

    // alert("Geocode was not successful for the following reason: " + status);
    document.getElementById('errorMessage').style.display = "block";               
}

Google map 在其最新版本中确实有一些不错的叠加层,但它主要围绕 map 上的位置和区域范围,而不是一般的文本信息。所以您会看到,您需要在 map 之外、 map 上方或下方的 HTML 页面上创建自己的 UI。如果您确实想将其显示在 map 顶部,可以在 errorMessage div 上设置以下 css 属性:

#errorMessage {
    position:absolute;
    left: 50%;
    top: 30%;
}

关于javascript - 简单的谷歌地图与地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8416364/

相关文章:

javascript - 使用 Knockout 动态创建菜单结构

javascript - 对象作为 JavaScript 中的变量容器

javascript - JQuery - 使用 fa fa-star 计算 i 的数量

javascript - 使用 fabricjs 将 "bleed area"添加到 Canvas ?

google-maps - 为什么 Geocode API 找不到这个地址,而 Google map 找到了?

javascript - 如何控制fitBounds是否改变边界?

javascript - 信息气泡始终显示在最后一个标记上

javascript - 使用 javascript 动态更改表行类名称

android - 如何像uber android应用程序一样在 map 上获取居中位置图钉的位置

javascript - 无法让多页jquery mobile与谷歌地图一起使用