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 找到附近的医院位置。
这是工作代码片段:
// 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% 工作只需使用它哪里
pickupLatitudes=96.766, pickupLongitudes=18.3456
类似这样的东西 输入重要的内容以获得回复,例如 =>
type=hospital,type=restaurant,type=atm
以下是如何使用 Google Maps API 访问 JSON/XML 端点。
获取您的纬度 = $latitude
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$latitude%2C$longitude&radius=1500&type=$TypeOfPlace&key=$APIKey"
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