Google地图未显示使用地理编码服务

时间:2015-10-03 21:58:29

标签: javascript google-maps google-geocoder

我正在尝试使用地理编码服务,但由于某种原因地图没有显示。当我检查开发者控制台时,它不会显示任何Javascript错误。

这是谷歌地图在Chrome中显示的方式。

enter image description here

<!DOCTYPE html>
        <html>
          <head>
            <title>Simple Map</title>
            <meta name="viewport" content="initial-scale=1.0">
            <meta charset="utf-8">
            <style>
              html, body {
                height: 100%;
                margin: 0;
                padding: 0;
              }
            </style>

            <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
            <script src="\div.js"></script>
        [![enter image description here][1]][1]
            <script>
            google.maps.visualRefefresh = true;

        var map;
        function initialize() {
            getCoordinates('287 West Center Street Utah', function(coords) {
                var mapOptions = {
                center: new google.maps.LatLng(coords[0],coords[1]),
                zoom: 8
                };
            map = new google.maps.Map(document.getElementById('map'), mapOptions);
            })
        }

        google.maps.event.addDomListener(window,'load',initialize);
            </script>
        </head>
        <body>
            <div id="map" style="height:100%"></div>        
          </body>
        </html>

div.js

    geocoder = new google.maps.Geocoder();
        function getCoordinates(address,callback) {
            var coordinates;
            geocoder.geocode({address : address} , function (results,status){
                coords_obj = results[0].geometry.location;
                coordinates = [coords_obj.nb, coords_obj.ob];
                callback(coordinates);
            })
        }

2 个答案:

答案 0 :(得分:1)

编辑1

改变这个:

function getCoordinates(address,callback) {
    var coordinates;
    geocoder.geocode({address : address} , function (results,status){
        coords_obj = results[0].geometry.location;
        coordinates = [coords_obj.nb, coords_obj.ob];
        callback(coordinates);
    })
}

为此:

function getCoordinates(address,callback) {
    var coordinates;
    geocoder.geocode({address : address} , function (results,status){
        coords_obj = results[0].geometry.location;
        coordinates = [coords_obj.H, coords_obj.L];
        callback(coordinates);
    })
}

您的错误:coordinates = [coords_obj.nb,coords_obj.ob];

编辑2

geocoder = new google.maps.Geocoder();

function getCoordinates(address, callback) {
  var coordinates;
  geocoder.geocode({
    address: address
  }, function(results, status) {
    coords_obj = results[0].geometry.location;
    console.log(coords_obj)
    coordinates = [coords_obj.H, coords_obj.L];
    callback(coordinates);
  })
}

google.maps.visualRefefresh = true;

var map;

function initialize() {
  getCoordinates('287 West Center Street Utah', function(coords) {
    var mapOptions = {
      center: new google.maps.LatLng(coords[0], coords[1]),
      zoom: 8
    };

    console.log(mapOptions)
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  })
}

google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

<div id="map" style="height:900px;width:1000px;"></div>

答案 1 :(得分:1)

不要使用Google Maps Javascript API v3的未记录属性。他们可以并且每次发布时都会改变。 始终使用记录的方法(.lat(),. lng())。

working fiddle

代码段::

&#13;
&#13;
google.maps.visualRefefresh = true;

var map;

function initialize() {
  getCoordinates('287 West Center Street Utah', function(coords) {
    var mapOptions = {
      center: new google.maps.LatLng(coords[0], coords[1]),
      zoom: 8
    };
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
  })

}

google.maps.event.addDomListener(window, 'load', initialize);
var geocoder = new google.maps.Geocoder();

function getCoordinates(address, callback) {
  var coordinates;
  geocoder.geocode({
    address: address
  }, function(results, status) {
    coords_obj = results[0].geometry.location;
    coordinates = [coords_obj.lat(), coords_obj.lng()];
    callback(coordinates);
  })
}
&#13;
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map" style="height:100%"></div>
&#13;
&#13;
&#13;

相关问题