gpt4 book ai didi

javascript - 我正在尝试制作一个餐厅定位应用程序,但无法显示标记

转载 作者:行者123 更新时间:2023-12-02 14:31:31 25 4
gpt4 key购买 nike

我正在尝试将餐馆的标记放在我的 map 上,但它不起作用。任何帮助,将不胜感激。是否需要添加查询方法?我也不太尝试,因为完成后我会让位置保持静态。这是我的相关代码,

<script>
var map;

function initialize() {
var center = new google.maps.LatLng(37.422, -122.084058);
map = new google.maps.Map(document.getElementById('map'), {
center: center,
zoom: 13
});

var request = {
location: center,
radius: 8047,
types: ['restaurant']
};

var service = new google.places.PlaceService(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.addDomListener(window, 'load', initialize);
</script>

最佳答案

PlacesService 需要 API_KEY

 var map;
var infowindow;

function initMap() {
var center = new google.maps.LatLng(37.422, -122.084058);

map = new google.maps.Map(document.getElementById('map'), {
center: center,
zoom: 13
});

infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: center,
radius: 8047,
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);
});
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
<!DOCTYPE html>
<html>

<head>
<title>Place searches</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<script>
</script>
</head>

<body>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB1tbIAqN0XqcgTR1-FxYoVTVq6Is6lD98&libraries=places&callback=initMap" async defer></script>
</body>

</html>

关于javascript - 我正在尝试制作一个餐厅定位应用程序,但无法显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37770424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com