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
问题:
回答1:
A degree of latitude is 60 nautical miles but obviously that goes out of the window for longitude.
Therefore it sounds like you would need to use the Great Circle Distance Equation in reverse.
GDC gives you the distance between to points on the Earth - it's complicated because it's not a straight line as you have to negotiate the curvature of the sphere.
If you could work the GDC in backwards you'd be able to work out the LatLong of a point 500m away from the current one.
(Obviously this only applies as the crowflies. A route following a road would be different. The Google directions service returns distance in metres though IIRC.)
回答2:
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; } }