如何在谷歌地图500米的距离放两个标记 [英] how to put two marker at a distance of 500m in google map
问题描述
我们如何在500米的距离放置两个标记,让第一个标记的latLng为伦敦(51,0),第二个标记放置在距离该标记500m的距离处。我已经尝试过任何事情,但无法找到并回答它..任何想法
How we can put two markers at a distance of 500 meter, lets say latLng for first marker is london (51,0) and second marker is put ad a distance of 500m from this marker. I have tried ever thing but couldnt find and answer for it.. any ideas
推荐答案
我已经查看了Google Maps API和它似乎我的回答是错误的。我没有注意到latLng不适用于长度而是经度和纬度(它看起来好像我忘记了地球不平坦:))。使用这个漂亮的网站,我能够想出更好的解决方案:
I have looked into Google Maps API and it appears that my answer was really wrong. I haven't noticed that latLng doesn't work with length but latitude and longitude (it looks as if i have forgotten that earth is not flat :) ). Using this nice web site I was able to came up with better solution:
var position = new google.maps.LatLng(51.517289, -0.130463);
var latlngs = [];
var map;
// distance from position in km
var d = 5;
// Earth radius
var R = 6372;
function initialize()
{
var mapOptions = { zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: position };
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
function dropMarkers()
{
var lat1Deg = position.lat();
var lon1Deg = position.lng();
var lat1Rad = lat1Deg.toRad();
var lon1Rad = lon1Deg.toRad();
latlngs.push(new google.maps.Marker({
position: position,
map: map,
draggable: false,
animation: google.maps.Animation.DROP}));
for (var brngDeg = 0; brngDeg < 360; brngDeg+=20)
{
brngRad = brngDeg.toRad();
var lat2Rad = Math.asin( Math.sin(lat1Rad)*Math.cos(d/R) +
Math.cos(lat1Rad)*Math.sin(d/R)*Math.cos(brngRad) );
var lon2Rad = lon1Rad +
Math.atan2(Math.sin(brngRad)*Math.sin(d/R)*Math.cos(lat1Rad),
Math.cos(d/R)-Math.sin(lat1Rad)*Math.sin(lat2Rad));
lat2Deg = lat2Rad.toDeg();
lon2Deg = lon2Rad.toDeg();
latlngs.push(new google.maps.Marker({
position: new google.maps.LatLng(lat2Deg,lon2Deg),
map: map,
draggable: false,
animation: google.maps.Animation.DROP}));
}
}
if (typeof(Number.prototype.toRad) === "undefined")
{
Number.prototype.toRad = function()
{
return this * Math.PI / 180;
}
}
if (typeof(Number.prototype.toDeg) === "undefined")
{
Number.prototype.toDeg = function()
{
return this * 180 / Math.PI;
}
}
这篇关于如何在谷歌地图500米的距离放两个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!