Html 如何使用谷歌地图api和javascript查找附近的医院
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18722477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find nearby hospitals using google map api and javascript
提问by Deepu S A
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'
});
},
};
I have the current position's latitude and longitude. How can i find nearby hospitals locations using only javascript and google maps api.
我有当前位置的纬度和经度。如何仅使用 javascript 和 google maps api 找到附近的医院位置。
回答by Suvi Vignarajah
You need to use the Places Library. In the Place Search Requestsyou can specify a type
- which categorizes what "type" of place you are searching for. According to the Supported Place Types by Google Places API, there are hospital
and health
types - which would probably be your best bet to get hospitals in your search response.
您需要使用Places Library。在地方搜索请求中,您可以指定一个type
- 它对您搜索的地方的“类型”进行分类。根据Google Places API 支持的地点类型,有hospital
和health
类型 - 这可能是您在搜索响应中获得医院的最佳选择。
回答by Rahul Desai
I had a chance to work on it recently.
我最近有机会在这方面工作。
Here is the working code snippet:
这是工作代码片段:
// 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>
It is importantto note that the places wouldnt be marked onto the map if you load the library and Javascript at the end of <body>
tag.
重要的是要注意,如果您在<body>
标签末尾加载库和 Javascript,则这些地点不会被标记到地图上。