如何使用谷歌地图API和javascript查找附近的医院

问题描述 投票:0回答:4
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 和谷歌地图 API 找到附近的医院位置。

javascript html google-maps-api-3
4个回答
20
投票

您需要使用地点库。在地点搜索请求中,您可以指定

type
- 它对您要搜索的地点“类型”进行分类。根据 Google Places API 支持的地点类型,有 hospital
health
类型 - 这可能是您在搜索响应中找到医院的最佳选择。
    


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>

重要的是要注意,如果您在

<body>

标记末尾加载库和 Javascript,则这些地点不会被标记到地图上。

这里是查找附近医院、餐厅等的网址
100% 工作只需使用它


2
投票

哪里

pickupLatitudes=96.766, pickupLongitudes=18.3456
类似这样的东西
输入重要的内容以获得回复,例如 =>
type=hospital,type=restaurant,type=atm

    
以下是如何使用 Google Maps API 访问 JSON/XML 端点。


0
投票
获取您的 API 密钥 = $APIKey

获取您的纬度 = $latitude
  1. 获取您的经度 = $经度
  2. 获取地点类型(医院、餐厅)等= $TypeOfPlace
  3. 使用以下 URL 访问 .JSON 端点:
  4. "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$latitude%2C$longitude&radius=1500&type=$TypeOfPlace&key=$APIKey"
  5. JSON 端点的示例是:
https://maps.googleapis.com/maps/api/place/nearbysearch/json
  ?keyword=cruise
  &location=-33.8670522%2C151.1957362
  &radius=1500
  &type=restaurant
  &key=YOUR_API_KEY

您还可以使用 cURL 发出 GET 请求。相同的示例是:

curl -L -X GET 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522%2C151.1957362&radius=1500&type=restaurant&keyword=cruise&key=YOUR_API_KEY'

有关 .JSON 端点的更多信息,请参阅文档:

https://developers.google.com/maps/documentation/places/web-service/search-nearby

    

© www.soinside.com 2019 - 2024. All rights reserved.