Google Maps API - 在美国生成随机cooridinates [英] Google Maps API - generate random cooridinates in USA
问题描述
是否有可能为谷歌地图API生成随机的经度和纬度,并以这些坐标为中心(我需要坐标在美国大陆部分的某个地方)?
首先,你需要确定美国大陆的界限。你可以通过这个网页获得 http://econym.org.uk/gmap/states.xml 。一个你有他们,你可以使用任何数据结构来填充,然后可以将它们存储在 get 变量中的变量中。然后使用以下代码生成随机坐标
var bounds = yourarray.getBounds();
map.fitBounds(bounds);
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast(); ($ i $);
for(var i = 0; i <100; i ++){
var ptLat = Math.random()*(ne.lat() - sw.lat())+ sw.lat );
var ptLng = Math.random()*(ne.lng() - sw.lng())+ sw.lng();
var point = new google.maps.LatLng(ptLat,ptLng);
if(google.maps.geometry.spherical.computeDistanceBetween(point,circle.getCenter())< circle.getRadius()){
createMarker(map,point,marker+ i);
以下是创建标记的函数
函数createMarker(map,point,content){
var marker = new google.maps.Marker({
position:point,
map:map
});
google.maps.event.addListener(marker,click,function(evt){
infowindow.setContent(content +< br>+ marker.getPosition()。toUrlValue(6) );
infowindow.open(map,marker);
});
返回标记;
}
google.maps.event.addDomListener(window,'load',initialize);
最后,调用fitBounds(标记)将标记居中并放大当前生成的标记。 p>
Is it possible to generate random latitude and longitude for Google maps API and to center on those coordinates (I need coordinates somewhere on continental part of USA)?
First of all you need to define the bounds of continental USA. You can get those with this webpage http://econym.org.uk/gmap/states.xml. One you have them you can use any data structure to populate and then can store them in a variable in a get variable. And then use the following code to generate random coordinates
var bounds = yourarray.getBounds();
map.fitBounds(bounds);
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
for (var i = 0; i < 100; i++) {
var ptLat = Math.random() * (ne.lat() - sw.lat()) + sw.lat();
var ptLng = Math.random() * (ne.lng() - sw.lng()) + sw.lng();
var point = new google.maps.LatLng(ptLat, ptLng);
if (google.maps.geometry.spherical.computeDistanceBetween(point, circle.getCenter()) < circle.getRadius()) {
createMarker(map, point, "marker " + i);
Here is the function to create marker
function createMarker(map, point, content) {
var marker = new google.maps.Marker({
position: point,
map: map
});
google.maps.event.addListener(marker, "click", function(evt) {
infowindow.setContent(content + "<br>" + marker.getPosition().toUrlValue(6));
infowindow.open(map, marker);
});
return marker;
}
google.maps.event.addDomListener(window, 'load', initialize);
Finally call the fitBounds(marker) to center and zoom the marker on the currently generated marker.
这篇关于Google Maps API - 在美国生成随机cooridinates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!