【问题标题】:How to find nearby hospitals using google map api and javascript如何使用 google map api 和 javascript 查找附近的医院
【发布时间】:2023-04-07 11:15:01
【问题描述】:
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 找到附近的医院位置。

【问题讨论】:

    标签: javascript html google-maps-api-3


    【解决方案1】:

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

    【讨论】:

    • 非常感谢您的回答。请让我知道您是否可以为基于 HTML5 + javascript + google map api 的模块等应用程序提供一些有用的链接或代码。我浏览了您的个人资料,很高兴知道您在开发此类应用程序方面拥有丰富的经验。如果您可以帮助我,请提供链接或代码示例。无论如何感谢你
    • 我认为 API 文档中的示例代码正是您所需要的:sample code。对于这个问题,您只需要编辑locationrequest 中的type 参数即可。这是我快速整理的link to a jsfiddle,演示了它是如何完成的。
    • 非常感谢苏维... :)
    • 我的经验是,医院类型非常过时,你最好通过关键字医院进行附近搜索,它会给你一个更好的匹配结果。例如,我们附近有一家梅奥诊所医院,但有一堆提供者在大楼内注册的类型医院也导致结果不准确。
    【解决方案2】:

    我最近有机会研究它。

    这里是工作代码sn-p:

    // 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>

    重要要注意,如果您在&lt;body&gt; 标记的末尾加载库和Javascript,这些地点不会被标记到地图上。

    【讨论】:

    • 如何获取从地图中检索到的所有标记?
    • @JoshHarington 在createMarker() 函数中,您可以在创建标记时将其推送到数组中。与markersArray.push(marker) 一样,markersArray 在这种情况下应该是全局的,并初始化为空数组[]
    【解决方案3】:

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

    【讨论】:

    • 欢迎来到 Stack Overflow!这是一个大约 8 年前的老问题。如果您还有其他问题,请点击 按钮进行提问。
    猜你喜欢
    • 1970-01-01
    • 2018-02-23
    • 2017-02-14
    • 1970-01-01
    • 2016-07-14
    • 2018-08-14
    • 2017-05-28
    • 2019-04-21
    • 2013-06-27
    相关资源
    最近更新 更多