【发布时间】:2020-10-07 14:52:57
【问题描述】:
我正在开展一个项目,该项目定位用户位置附近的咖啡店(通过浏览器检索)。我正在使用带有地理位置的 Google Maps API。
我的想法是通过地理定位来定位用户的位置,然后检索他们所在位置附近的咖啡馆。 一旦浏览器检索到位置,我似乎无法弄清楚如何添加咖啡店或自动搜索并在用户附近的地图上显示咖啡店。
硬编码其中的位置似乎不是解决方案,但我希望从更有经验的程序员那里获得一些关于如何做到这一点的建议。
目前,地图的代码取自 Google 地图文档,因为它提供了我需要的搜索以外的功能。
我已经贴在下面了。
function mapFunc() {
// referenced from google maps api documentation
var map;
var infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: -34.397,
lng: 150.644
},
zoom: 15
});
infoWindow = new google.maps.InfoWindow;
//HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('You are here!');
infoWindow.open(map);
map.setCenter(pos);
}, function () {
handleLocationError(true, infoWindow, map.getCenter());
search();
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
//get coffee shops nearby??
}
mapFunc();
【问题讨论】:
标签: javascript google-maps google-maps-api-3 google-places-api google-geolocation