我正在实现谷歌地图..我想要当前位置的默认谷歌蓝点。任何人都可以告诉,如何在 reactjs 或简单的 javascript 中这样做..
最佳答案
首先您必须允许浏览器将您的地理位置发送到 Google API,然后在窗口/文档加载事件上连接此代码:
var map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: -34.397, lng: 150.644 },
zoom: 6
});
infoWindow = new google.maps.InfoWindow;
// Try 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('Location found.');
infoWindow.open(map);
var marker = new google.maps.Marker({
position: pos,
map: map,
title: 'Hello World!',
icon: 'https://maps.google.com/mapfiles/kml/shapes/info-i_maps.png',
});
map.setCenter(pos);
}, function () {
//handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
//handleLocationError(false, infoWindow, map.getCenter());
}
}
关于javascript - 谷歌地图蓝点在 reactjs 中的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51463936/