2017-02-28 60 views
2

我使用谷歌地图,使地图上的标记,我试图将地址转换使用下面的代码地址解析:遗漏的类型错误:无法读取属性未定义的谷歌地图“应用”

<script src="https://maps.googleapis.com/maps/api/js?key=&callback=initialize" async defer></script> 

<script type="text/javascript"> 
var map; 

function initialize() { 
    var chamberLocation = {lat: 43, lng: -82}; 
    var geocoder = new google.maps.Geocoder(); 
    map = new google.maps.Map(document.getElementById('map'), { 
    center: {lat: 42.9745, lng: -82.4066}, 
    zoom: 14, 
    styles: [{"featureType":"road","elementType":"geometry","stylers":[{"visibility":"simplified"}]},{"featureType":"road.arterial","stylers":[{"hue":149},{"saturation":-78},{"lightness":0}]},{"featureType":"road.highway","stylers":[{"hue":-31},{"saturation":-40},{"lightness":2.8}]},{"featureType":"poi","elementType":"label","stylers":[{"visibility":"off"}]},{"featureType":"landscape","stylers":[{"hue":163},{"saturation":-26},{"lightness":-1.1}]},{"featureType":"transit","stylers":[{"visibility":"off"}]},{"featureType":"water","stylers":[{"hue":3},{"saturation":-24.24},{"lightness":-38.57}]}], 
    zoomControl: false, 
    scaleControl: false, 
    mapTypeControl: false, 
    disableDefaultUI: false, 
    streetViewControl: false, 
    rotateControl: false, 
    scrollwheel: false, 
    draggable: false 
    }); 
    codeAddress(geocoder, map); 

    } 

    function codeAddress(geocoder, map) { 
     var address = 'place'; 
     geocoder.geocode({ 'address' : address }), function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       var marker = new google.maps.Marker({ 
        map: map, 
        position: results[0].geometry.location 
       }); 
      } else { 
       console.log('This didnt work' + status); 
      } 
     }; 
    } 


</script> 

每当我这样做我得到一个错误说Uncaught TypeError: Cannot read property 'apply' of undefined

什么是造成这个错误?我不确定如何解决这个问题。我必须导入另一个Google地图API吗?

回答

4

错误是一个错字。我在下面的代码中输入了代码//change,并在输入错误的地方发表了评论。 geocoder.geocode({}, callback)是一个函数,它接受一个对象和回调,但你必须geocoder.geocode({ 'address' : address }),错字是)应该geocoder.geocode({ 'address' : address }, function(results, status) { ...

<div id="map" style="width: 320px; height: 480px;"></div> 

    <script type="text/javascript"> 
    var map; 

    function initialize() { 
     // your code 
     // etc ... 
     codeAddress(geocoder, map); 
    } 

    function codeAddress(geocoder, map) { 
     var address = 'place'; 
     geocoder.geocode({ 
      'address': address 
     }, // change 
     function(results, status) { 
      if (status == 'OK') { // change 

      var marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
      }); 

      // some debug output 
      console.log("status is: " + status) 
      console.log("results is: " + JSON.stringify(results[0].geometry.location)) 
      } else { 
      console.log('This didnt work' + status); 
      } 
     }); 
    }; 
    </script> 

    <script async defer src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initialize"></script> 
相关问题