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

Dee*_*S A 6 javascript html5 google-maps-api-3

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'
      });
   }, 
}; 
Run Code Online (Sandbox Code Playgroud)

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

Suv*_*jah 11

您需要使用地方库.在地方搜索请求中,您可以指定type- 对您要搜索的地点的"类型"进行分类.根据Google Places API支持的地点类型,有一些hospitalhealth类型 - 这可能是您在搜索响应中获得医院的最佳选择.


Rah*_*sai 6

我最近有机会在这方面工作。

这是工作代码片段:

// comments inline
Run Code Online (Sandbox Code Playgroud)

// comments inline
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
html, body, #map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
Run Code Online (Sandbox Code Playgroud)

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