JavaScript - How to create random longitude and latitudes?

后端 未结 6 1338
礼貌的吻别
礼貌的吻别 2020-12-24 12:47

I am trying to create random longitude and latitudes. I need to create numbers between -180.000 and +180.000. So, I might get 101.325 or -3.546 or -179.561.

Can y

6条回答
  •  长发绾君心
    2020-12-24 13:01

    Math.random()*360 - 180
    

    that will get you a range of -180 to 180

    And if you really only want 3 decimal places

    Math.round((Math.random()*360 - 180) * 1000)/1000
    

提交回复
热议问题