如何自定义附近地点搜索返回的结果,Google Maps JS API V3

Posted

技术标签:

【中文标题】如何自定义附近地点搜索返回的结果,Google Maps JS API V3【英文标题】:How to customize results returned in nearby place search, Google Maps JS API V3 【发布时间】:2016-10-30 09:41:28 【问题描述】:

我对 Google Maps API 和 JS 还比较陌生,所以这可能(可能) 有一个简单的答案。

我现在返回我所在位置一定半径内的所有地点 已设置,但我想更具体一些,例如仅苗圃、加油站和健身房,并且仅显示这些标记。

抱歉,代码块太长了,如果您愿意,这里有一个 JSBin https://jsbin.com/yodolexece/edit?html,js,output

提前致谢。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Locator</title>

    <script type="text/javascript"       
     src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBdodiLO598_RD8_NYXK7nBKNA9Fhx_uBQ&libraries=places,geometry&.js"></script>

    <script 
        src="https://code.jquery.com/jquery-3.1.1.js"
        integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
        crossorigin="anonymous"></script>
    </head>
    <body>
      <input id="findMe" type="button" value="find closest place">
      <div id="map-canvas" style="height:500px;"></div>
     </body>
    </body>
  </html>

JS:

<script>   
jQuery(function($) 
var $overlay = $('.overlay'),
  resize = true,
  map;
var service;  
var marker = [];
var pos;
var infowindow;
var placeLoc

function initialize() 
  var mapOptions = 
    zoom: 15
  ;
  map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

  // Try HTML5 geolocation
  if (navigator.geolocation) 
    navigator.geolocation.getCurrentPosition(function(position) 
      var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
      $('#findMe').data('pos', pos);
      var request = 
        location: pos,
        radius: 1000,
      ;
      infowindow = new google.maps.InfoWindow();
      var service = new google.maps.places.PlacesService(map);
      service.nearbySearch(request, callback);
      infowindow = new google.maps.InfoWindow(
        map: map,
        position: pos,
        content: 'You Are Here'
      );
      map.setCenter(pos);
    , function() 
      handleNoGeolocation(true);
    );
   else 
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  

  function callback(results, status) 
    var markers = [];
    if (status == google.maps.places.PlacesServiceStatus.OK) 
      for (var i = 0; i < results.length; i++) 
        markers.push(createMarker(results[i]));
      
    
    $('#findMe').data('markers', markers);
  


function createMarker(place) 
  placeLoc = place.geometry.location;
  var marker = new google.maps.Marker(
    map: map,
    type: ['store'],
    position: place.geometry.location,
    icon: 
      path: google.maps.SymbolPath.CIRCLE,
      scale: 8,
      fillColor: '00a14b',
      fillOpacity: 0.3,
      fillStroke: '00a14b',
      strokeWeight: 4,
      strokeOpacity: 0.7
    ,
  );
  google.maps.event.addListener(marker, 'click', function() 
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  );
  return marker;


function handleNoGeolocation(errorFlag) 
  if (errorFlag) 
    var content = 'Error: The Geolocation service failed.';
   else 
    var content = 'Error: Your browser doesn\'t support geolocation.';
  

  var options = 
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
  ;
  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);

google.maps.event.addDomListener(window, 'load', initialize);

$('#show').click(function() 
  $overlay.show();
  if (resize) 
    google.maps.event.trigger(map, 'resize');
    resize = false;
  
);

$('.overlay-bg').click(function() 
  $overlay.hide();
);

$("#findMe").click(function() 
  var pos = $(this).data('pos'),
    markers = $(this).data('markers'),
    closest;
  if (!pos || !markers) 
    alert('pos or markers not set yet');
    return;
  
  $.each(markers, function() 
    var distance = google.maps.geometry.spherical.computeDistanceBetween(this.getPosition(), pos);
    if (!closest || closest.distance > distance) 
      closest = 
        marker: this,
        distance: distance
      
    
  );
  if (closest) 
    google.maps.event.trigger(closest.marker, 'click')
  
);
);
</script>

【问题讨论】:

【参考方案1】:

这很简单。您需要传递一个类型数组,您需要使用一个名为 types 的新属性来编辑您的请求以进行过滤。 Ex 类型:['bank', 'gym']

下面的代码块,我会附上你的 JS Bin 的修改版本

JS Bin

地点类型可以从下面的链接中找到

Place Types

Google Places API 文档 - developers.google.com/maps/documentation/javascript/places#place_search_requests

var request = 
            location: pos,
            radius: 1000,
            types: ['bank', 'gym']
          ;
          infowindow = new google.maps.InfoWindow();
          var service = new google.maps.places.PlacesService(map);
          service.nearbySearch(request, callback);

【讨论】:

我非常感谢你,以为这很容易,但那非常容易。你让我度过了周末。 没问题..很高兴能帮到你

以上是关于如何自定义附近地点搜索返回的结果,Google Maps JS API V3的主要内容,如果未能解决你的问题,请参考以下文章

google 的缓存策略将 api 放置在结果附近。

形成 Google Maps URL,用于搜索特定坐标附近的特定地点

使用适用于 android 的 google places api 网络服务搜索特定的附近地点

Google Maps API V3 - 边界内最近的路线

Android。点击标记后的Google map API工具

Google 搜索地点 api,未找到地点