谷歌地图API使用Math.random获取随机位置 - JS

Google Maps Api using Math.random to get random location - JS

本文关键字:随机 位置 JS 获取 random API 使用 Math 谷歌地图      更新时间:2023-09-26

我无法让我的地图转到随机位置。我希望它转到边界之间的随机位置(纬度 -85 到 85,lng -180 到 180)。每当单击该按钮时,该功能都会使地图变灰。

function random(lat, lng){
var random = new google.maps.LatLng((Math.random()-85)*85+, (Math.random()-180)*180));
map.panTo(random);
}
var random = new google.maps.LatLng( (Math.random()*(85*2)-85), (Math.random()*(180*2)-180) );

尝试这样的东西,或更一般地说:

function randomBetween(min, max) {
if (min < 0) {
    return min + Math.random() * (Math.abs(min)+max);
}else {
    return min + Math.random() * max;
}}