Google maps API Uncaught TypeError:无法读取属性' lat'未定义的

时间:2016-09-05 19:05:00

标签: javascript jquery google-maps-api-3

我在Chrome开发工具中遇到此错误,即使应用程序已成功加载,但一切似乎都运行正常但是,这个错误真的让我困扰,我似乎无法找到&# 39;造成它。

var mapDiv = document.getElementById('map');
var map;
var pos;
var infowindow;
var search_place;

$.get( "http://ip-api.com/json", function(data) {
    pos = new google.maps.LatLng(data.lat, data.lon);
    initMap();
  });

function initMap() {
  map = new google.maps.Map(mapDiv, {
    zoom: 15,
    center: pos,
    mapTypeControl: false
  });

  // Display user's current location on the map
  var curr_marker = new google.maps.Marker({
    position: pos,
    map: map,
    draggable: false,
   animation: google.maps.Animation.DROP
  });

  // Display a label that indicates user's current location
  var contentString = '<div id="content">'+
      '<h3>' + 'This is you!' + '</h3>' +
      '</div>';
  var curr_infowindow = new google.maps.InfoWindow();
  curr_infowindow.setContent(contentString);

  infowindow = new google.maps.InfoWindow();

  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch({
    location: {lat: pos.lat(), lng: pos.lng()},
    radius: 1000,
    type: ['restaurant']
  }, callback);
  ...
  ...
  ...

1 个答案:

答案 0 :(得分:0)

事实证明,当我调用API时,我不需要回调元素。

在:

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

后:

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