9
var MapApiApplication = {
   myCurrentPosition : "",
   mapOptions : "",
   marker : "",
   initialize : function(){
      MapApiApplication.myCurrentPosition = new google.maps.LatLng(10.112293000000000000, 76.352684500000010000);
      MapApiApplication.mapOptions = {
        zoom: 13,
        center: MapApiApplication.myCurrentPosition,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      MapApiApplication.map = new google.maps.Map(document.getElementById('map-canvas'), MapApiApplication.mapOptions);
      MapApiApplication.marker = new google.maps.Marker({
         position: MapApiApplication.myCurrentPosition,
         map: MapApiApplication.map,
         title: 'Here You are'
      });
   }, 
}; 

我有当前位置的纬度和经度。如何仅使用 javascript 和 google maps api 找到附近的医院位置。

4

3 回答 3

19

您需要使用Places Library。在Place Search Requests中,您可以指定type- 对您要搜索的地点的“类型”进行分类。根据Google Places API 的 Supported Place Types,有hospitalhealth类型 - 这可能是您在搜索响应中获得医院的最佳选择。

于 2013-09-10T15:14:40.927 回答
6

我最近有机会研究它。

这是工作代码片段:

// comments inline

var map;
var infowindow;

function initialize() {
  var pyrmont = new google.maps.LatLng(19.107567, 72.8335); // sample location to start with: Mumbai, India

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

  var request = {
    location: pyrmont,
    radius: 200,
    types: ['hospital', 'health'] // this is where you set the map to get the hospitals and health related places
  };
  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);
html, body, #map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>

<div id="map-canvas"></div>

请务必注意,如果您在标签末尾加载库和 Javascript,则这些地点不会被标记到地图上<body>

于 2015-01-27T15:06:35.357 回答
0

实际上我需要一个 api 来找到我附近的医院。问题是我需要它来实现 thunkable。你能告诉我如何使用上面的 api(https://maps.googleapis.com/maps/api/js?v=3 .exp&libraries=places ) 相同

于 2021-08-05T18:30:35.420 回答