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找到附近的医院位置。
答案 0 :(得分:9)
您需要使用Places Library。在Place Search Requests中,您可以指定type
- 对您要搜索的地点的“类型”进行分类。根据{{3}},有hospital
和health
类型 - 这可能是您在搜索回复中获得医院的最佳选择。
答案 1 :(得分:3)
我最近有机会参与其中。
以下是工作代码段:
// 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);
&#13;
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
<div id="map-canvas"></div>
&#13;
重要要注意,如果您在<body>
标记的末尾加载库和Javascript,那么这些地方就不会被标记到地图上。
答案 2 :(得分:0)
实际上我需要一个 api 来找到我附近的医院。问题是我需要它来 thunkable。你能告诉我如何使用上面的 api(https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places) 吗