谷歌地图API找到附近的餐厅周围我的地方
Google Map API to find nearby restaurant around my place
我正在开发谷歌地图api[这是我在餐馆附近找到的……当我运行这个程序时,我只得到当前位置。我没有收到任何与餐馆数据有关的信息。
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 25.276987, lng: 55.296249 },
zoom: 15
});
var infoWindow = new google.maps.InfoWindow({ map: map });
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
var marker = new google.maps.Marker({
map: map,
position: pos.getPlace().location
});
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: rak,
radius: 5500,
type: ['restaurant'] // not resturant
}, callback);
}, function () {
handleLocationError(true, infoWindow, map.getCenter());
});
}
else {
handleLocationError(false, infoWindow, map.getCenter());
}
}
也许您遇到了nearbySearch位置标记的错误。您没有初始化变量rak
,它将返回一个空值。
设置脚本
<script
src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&callback=initMap"
async defer></script>
初始化变量
var map;
var infowindow;
var myPlace = {lat: 25.276987, lng: 55.296249 };
呼叫您的nearBySearch
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location : myPlace,
radius : 5500,
type : [ 'restaurant' ]
}, 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);
});
}
您可以在本文档中找到这段代码,它将很好地解释正确的编码以及如何使用api。这里还有支持的位置类型列表。
我希望这能有所帮助。
相关文章:
- 无法在图像周围添加按钮
- 如何在每个动态创建的ImageButton周围包装超链接?Visual Studio
- 为什么不是't这=>边界“;这个“;到周围的范围
- 如何在输入中选择的文本周围包装html标记
- 在html页面的属性周围添加了一个额外的空间
- Javascript选择周围的所有元素
- CSS 在悬停时使图像变暗 - 在其周围添加一个 href
- 从周围的 HTML 调用 SVG 中的 JavaScript 函数
- 提取周围方法的 JavaScript 版本
- 如何展开选择以包括周围的链接标记
- 使用D3.js的SVG圆周围的矩形之间的间隙不是通用的
- HTML5画布的包装:如何使(静态)形状在画布边缘周围继续
- 为什么只有当我在字符串周围输入引号时,表单输入的isNaN才为true
- JavaScript数组周围的引号(可能很容易修复)
- 当用户向下滚动我的网页时,如何删除chrome地址栏周围的边框
- 如何在Javascript中在桌子周围随机移动图像
- 如何在鼠标光标周围创建形状
- 单击时如何在按钮周围保留矩形边框
- 如何删除字符串周围的特定字符
- 谷歌地图API找到附近的餐厅周围我的地方