javascript - 设置 Google map 以在加载时对邮政编码进行地理编码

标签 javascript function google-maps

我使用 Google Maps API 显示基于邮政编码的 map ,然后使用 Google 的地理编码服务 ( https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple ) 将邮政编码转换为纬度和经度并显示 map 。

Google 页面上的示例使用按钮来激活地理编码功能,我已使用该代码作为起点。但是,我不需要按钮,我想对邮政编码进行硬编码(稍后我将用从自定义元输入中获取的字符串替换它)。我如何调整此代码以获取我在地址变量中声明的邮政编码并在初始加载时加载它,而不是单击按钮?

希望这是有道理的。

<script src="http://maps.googleapis.com/maps/api/js?key=XXX&sensor=false" type="text/javascript"></script>
                                    <script>
                                        var geocoder;
                                        var map;
                                        function initialize() {
                                          geocoder = new google.maps.Geocoder();
                                          var latlng = new google.maps.LatLng(-34.397, 150.644);
                                          var mapOptions = {
                                            zoom: 8,
                                            center: latlng,
                                            mapTypeId: google.maps.MapTypeId.ROADMAP
                                          }
                                          map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                                        }

                                        function codeAddress() {
                                          var address = 'PO19 1DQ';
                                          geocoder.geocode( { 'address': address}, function(results, status) {
                                            if (status == google.maps.GeocoderStatus.OK) {
                                              map.setCenter(results[0].geometry.location);
                                              var marker = new google.maps.Marker({
                                                  map: map,
                                                  position: results[0].geometry.location
                                              });
                                            } else {
                                              alert('Geocode was not successful for the following reason: ' + status);
                                            }
                                          });
                                        }

                                        google.maps.event.addDomListener(window, 'load', initialize);
                                    </script>
                                    <input type="button" value="Geocode" onclick="codeAddress()">
                                    <div id="map-canvas" style="width:100%;height:300px;"></div>

最佳答案

只需从initialize()函数的底部调用codeAddress即可,例如:

function initialize() {
         geocoder = new google.maps.Geocoder();
         var latlng = new google.maps.LatLng(-34.397, 150.644);
         var mapOptions = {
          zoom: 8,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
         }
         map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
         codeAddress();
}

关于javascript - 设置 Google map 以在加载时对邮政编码进行地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17725403/

相关文章:

javascript - 使用jquery顺序调用多个函数

javascript - 如何在 Phaser 游戏中向每个用户显示不同的 map

javascript - 在 React 中输入焦点更改父类

c - 从数组中访问变量全局结构

javascript - 当用户按下提交时停止表单空白

android - 将 android-maps-utils 与 ADT 结合使用

ios - 如何在 iOS 中移动标记以及移动 Google map ?

javascript - JQuery 更新外部文件中的父类

javascript - 获取嵌套对象的值

javascript - 使用 JSON.stringify 时将原型(prototype)函数添加回返回的 Google Maps API 对象