问题
Is it possible with javascript to generate random latitude and longitude based on the user current position? I want to generate some random latitude and longitude positions after that the user is geolocated to place some markers on a leaflet map.
回答1:
Yes you can, Code below:
var latBounds = [-122, -77];
var lngBounds = [30, 50];
var features = [];
for( var i=0; i<80000; i++ ){
var lat = Math.random() * (latBounds[1]- latBounds[0] + 1) + latBounds[0];
var lng = Math.random() * (lngBounds[1]- lngBounds[0] + 1) + lngBounds[0];
features.push({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [lat, lng]
},
"properties": {
"title": "point_" +i,
"marker-symbol": "harbor"
}
});
}
var geoJSON = {
"type": "FeatureCollection",
"features": features
};
来源:https://stackoverflow.com/questions/63944488/generate-random-latitude-and-longitude-with-javascript-after-geolocation