javascript - Random marker within a rectangular bounds? - Stack Overflow

this is my code :function getRandom_marker(bounds) {var leftBottom=[bounds.getSouthWest().lat(),bounds.

this is my code :

function getRandom_marker(bounds) {
  var leftBottom=[bounds.getSouthWest().lat(),bounds.getSouthWest().lng()]
  var rightTop=[bounds.getNorthEast().lat(),bounds.getNorthEast().lng()]
  var latlng=[leftBottom[0]+Math.floor(Math.random() * (rightTop[0]-leftBottom[0])),
    leftBottom[1]+Math.floor(Math.random() * (rightTop[1]-leftBottom[1]))]
  return latlng
 }

i use this code to Generate a random , but sometimes, the marker is not in the map bounds,

so , what's wrong with my code ?

thanks

updated:

this is the code has Math.abs :

/

and this is not has:

/

this is my code :

function getRandom_marker(bounds) {
  var leftBottom=[bounds.getSouthWest().lat(),bounds.getSouthWest().lng()]
  var rightTop=[bounds.getNorthEast().lat(),bounds.getNorthEast().lng()]
  var latlng=[leftBottom[0]+Math.floor(Math.random() * (rightTop[0]-leftBottom[0])),
    leftBottom[1]+Math.floor(Math.random() * (rightTop[1]-leftBottom[1]))]
  return latlng
 }

i use this code to Generate a random , but sometimes, the marker is not in the map bounds,

so , what's wrong with my code ?

thanks

updated:

this is the code has Math.abs :

http://jsfiddle/PNYCf/1/

and this is not has:

http://jsfiddle/pRjBr/

Share Improve this question edited Sep 6, 2010 at 2:30 zjm1126 asked Sep 6, 2010 at 1:49 zjm1126zjm1126 35.7k53 gold badges125 silver badges167 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You may want to try the following:

function getRandom_marker(bounds) {
  var lat_min = bounds.getSouthWest().lat(),
      lat_range = bounds.getNorthEast().lat() - lat_min,
      lng_min = bounds.getSouthWest().lng(),
      lng_range = bounds.getNorthEast().lng() - lng_min;

  return new google.maps.LatLng(lat_min + (Math.random() * lat_range), 
                                lng_min + (Math.random() * lng_range));
}

Complete example:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps Random Points Demo</title> 
   <script src="http://maps.google./maps/api/js?sensor=false" 
           type="text/javascript"></script> 
</head> 
<body> 
   <div id="map" style="width: 400px; height: 300px;"></div> 

   <script type="text/javascript"> 

   function getRandom_marker(bounds) {
     var lat_min = bounds.getSouthWest().lat(),
         lat_range = bounds.getNorthEast().lat() - lat_min,
         lng_min = bounds.getSouthWest().lng(),
         lng_range = bounds.getNorthEast().lng() - lng_min;

     return new google.maps.LatLng(lat_min + (Math.random() * lat_range), 
                                   lng_min + (Math.random() * lng_range));
   }

   var map = new google.maps.Map(document.getElementById('map'), {
     zoom: 8,
     center: new google.maps.LatLng(-34.397, 150.644),
     mapTypeId: google.maps.MapTypeId.ROADMAP
   });

   google.maps.event.addListener(map, 'tilesloaded', function () {
     var mapBounds = map.getBounds();

     for (var i = 0; i < 20; i++) {
       new google.maps.Marker({
         position: getRandom_marker(mapBounds), 
         map: map
       });   
     }
   });

   </script> 
</body> 

Screenshot:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742387990a4434446.html

相关推荐

  • javascript - Random marker within a rectangular bounds? - Stack Overflow

    this is my code :function getRandom_marker(bounds) {var leftBottom=[bounds.getSouthWest().lat(),bounds.

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信