Generate random latitude and longitude with javascript after geolocation

吃可爱长大的小学妹 提交于 2021-01-29 20:59:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!