Google Places API库使用时没有地图--javascript

Ste*_*nin 8 javascript google-maps google-places-api

我需要从特定坐标获得距离半径100米的最近的场所.我找到了Google Places API示例代码,但我没有在我的应用程序中使用地图,因为此结果将在列表中呈现.这是API代码:

var map;
var infowindow;

function initialize() {
  var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);

  map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: pyrmont,
    zoom: 15
  });

  var request = {
    location: pyrmont,
    radius: 500,
    types: ['store']
  };
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}

function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
Run Code Online (Sandbox Code Playgroud)

这里的某个人发现你可以使用一个节点来渲染属性,但当我尝试使用它渲染时:

var $attrib = $('<div id="attributions"></div>');
$('#main').append($attrib);
...
service = new google.maps.places.PlacesService($attrib);
service.nearbySearch(request, callback);
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

TypeError: this.j[Wb] is not a function
Run Code Online (Sandbox Code Playgroud)

来自API库内部的某个地方.我希望这足以解决问题

Dr.*_*lle 5

$attrib 可能不是Node,我想这是一个jQuery对象.

使用

service = new google.maps.places.PlacesService($attrib[0]);
Run Code Online (Sandbox Code Playgroud)

或者没有jQuery:

service = new google.maps.places
          .PlacesService(document.getElementById('main')
                         .appendChild(document.createElement('div')));
Run Code Online (Sandbox Code Playgroud)