地理位置定位后,使用javascript生成随机纬度和经度 [英] Generate random latitude and longitude with javascript after geolocation

查看:124
本文介绍了地理位置定位后,使用javascript生成随机纬度和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript是否可以根据用户当前位置生成随机的纬度和经度?在用户进行地理位置定位以在传单地图上放置一些标记之后,我想生成一些随机的纬度和经度位置.

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.

推荐答案

可以,代码如下:

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
  };

这篇关于地理位置定位后,使用javascript生成随机纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆