Google Places API库在没有地图的情况下使用 - javascript [英] Google Places API library use without map - javascript

查看:111
本文介绍了Google Places API库在没有地图的情况下使用 - javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  var map; 
var infowindow;

函数initialize(){
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
$ b $ map = new google.maps.Map(document.getElementById('map-canvas'),{
center:pyrmont,
zoom:15
}) ;

var request = {
位置:pyrmont,
半径:500,
类型:['store']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request,callback);
}

函数回调(结果,状态){
if(status == google.maps.places.PlacesServiceStatus.OK){
for(var i = 0; i< results.length; i ++){
createMarker(results [i]);




函数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);

此处有人发现您可以使用节点将属性渲染为
,但是当我尝试ti渲染时:

  var $ attrib = $('< div id =attributions><< ; / DIV>'); 
$('#main')。append($ attrib);
...
service = new google.maps.places.PlacesService($ attrib);
service.nearbySearch(request,callback);

我收到错误消息:

  TypeError:this.j [Wb]不是函数

from在API库的深处。
我希望这是足够的信息来解决问题

解决方案

$ attrib 可能不是Node,我想它是一个jQuery对象。



使用

  service = new google.maps.places.PlacesService($ attrib [0]); 

或不包含jQuery:

  service = new google.maps.places 
.PlacesService(document.getElementById('main')
.appendChild(document.createElement('div')));


I need to get the closest establishments in 100m radius from specific coordinates. I found the Google Places API sample code, but I don't use a map in my application, since this results will be rendered in a list. Here is the API code:

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);

Someone here has found that you can use a node to render the attribution into, but when I try ti render in it:

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

I get an error:

TypeError: this.j[Wb] is not a function

from somewhere deep inside the API library. I hope this is enough of information to figure the problem out

解决方案

$attrib probably is not a Node, I guess it's a jQuery-object.

Use

service = new google.maps.places.PlacesService($attrib[0]);

or without jQuery:

service = new google.maps.places
          .PlacesService(document.getElementById('main')
                         .appendChild(document.createElement('div')));

这篇关于Google Places API库在没有地图的情况下使用 - javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆