4

背景:

我試圖使用谷歌地圖ground overlay但我們缺少GPS位置。如果我知道中心和左上方的GPS位置,我如何獲得矩形的經度和緯度?

我們有一張覆蓋圖的地圖,繪製在Google地圖屏幕截圖上。從此屏幕截圖中,我們記錄了左上角和中心中心的位置。

我們需要右下方的位置才能準確覆蓋這些圖像。請看下圖:

Top Left positions and center positions GPS known

的INTIAL思路是尋找兩個點之間的差異,並補充說,到中心點。

嘗試邏輯:

在JavaScript:

var topLeft  = [-32.8830055, 151.686214]; 
var centerCenter = [-32.9293803, 151.756686]; 

var difference1 = centerCenter[0] - ((topLeft[0] - centerCenter[0])) ; 
var difference2 = centerCenter[1] - ((topLeft[1] - centerCenter[1])) ; 

// being bottom right this should be "bigger" than above values 
// outputs [-32.97575509999999, 151.827158]; 
var bottomRight = [difference1 , difference2]; 

問題:

但是看來這就是地球的曲線拋出簡單數學出了門。我猜測下面發生的事情是爲什麼。

The image overlay is flat while the GPS system is curved.

所以給我一個矩形覆蓋,我知道左上點和中心點我可以計算出右下點即X上圖中的經度和緯度。注意我不知道這個矩形的真實世界距離。

注:我也知道,我將不得不將這些更改爲NW/SE以使用地面疊加層。

+1

希望這有助於:http://www.cpearson.com/excel/LatLong.aspx –

+0

你知道什麼「正確」的答案是,還是有一個例子覆蓋這個問題? – geocodezip

+0

@geocodezip它看起來應該是: -32.975755064903176,151.82719498450695基於下面的答案使用幾何庫。 – jnowland

回答

3

方法(使用幾何庫):

  1. 計算從西北標題居中

    google.maps.geometry.spherical.computeHeading(northwest, center); 
    
  2. 從西北計算距離到中心

    google.maps.geometry.spherical.computeDistanceBetween(northwest, center); 
    
  3. 東南計算使用

    google.maps.geometry.spherical.computeOffset(center, 
                  calculatedDistance, 
                  calculatedHeading); 
    

function initialize() { 
 

 
    var nw = new google.maps.LatLng(62.400471, -150.287132), 
 
    center = new google.maps.LatLng(62.341145, -150.14637), 
 
    map = new google.maps.Map(document.getElementById('map_canvas'), { 
 
     zoom: 9, 
 
     center: center 
 
    }), 
 
    heading, distance, se; 
 

 
    heading = google.maps.geometry.spherical.computeHeading(nw, center); 
 
    distance = google.maps.geometry.spherical.computeDistanceBetween(nw, center); 
 
    se = google.maps.geometry.spherical.computeOffset(center, distance, heading); 
 

 
    new google.maps.Marker({ 
 
    map: map, 
 
    position: center 
 
    }); 
 
    new google.maps.Marker({ 
 
    map: map, 
 
    position: nw 
 
    }); 
 
    new google.maps.Marker({ 
 
    map: map, 
 
    position: se 
 
    }); 
 

 
    new google.maps.GroundOverlay(
 
    'https://developers.google.com/maps/documentation/' + 
 
    'javascript/examples/full/images/talkeetna.png', { 
 
     north: nw.lat(), 
 
     south: se.lat(), 
 
     west: nw.lng(), 
 
     east: se.lng() 
 
    }, { 
 
     map: map 
 
    }); 
 

 

 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize);
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
}
<div id="map_canvas"></div> 
 
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>

1

您可能需要Haversine公式進行此類計算。所涉及的數學顯然處於比通常的答案更高的水平,儘管如此,您的問題實際上可能屬於gis或數學堆棧交換,其中一些更有經驗的SO用戶可能會給您更詳細的答案/示例。

2

你是否反對使用已經支持Haversine計算中心點的庫? (由於測試啓動)

退房Geolib:https://github.com/manuelbieh/Geolib

var center = geolib.getCenter([ 
    {latitude: 52.516272, longitude: 13.377722}, 
    {latitude: 51.515, longitude: 7.453619}, 
    {latitude: 51.503333, longitude: -0.119722} 
]); 

console.log(center); 

// Output: 
// 
// { 
//  "latitude": "52.009802", 
//  "longitude": "6.629000", 
//  "distance": 932.209 
// } 

一個codepen演示中,你可以玩:http://codepen.io/anon/pen/grbGrz/?editors=1010

你總是可以削減下來到你需要的功能。

+0

這個插件讓我知道中間點和角落之間的距離。 我基本上需要反轉getCenter函數,因爲我有結果。我只是不知道值的輸入。 – jnowland

+0

你是對的。我完全倒退地看完了。 – AgentK

+0

AgentK我想問一些問題。當我這樣做'geolib.convertUnit('km',14213,2)'它會自動對它做'Haversine'計算? – Gardezi

1

免於從您的查詢開始。你有兩個latlng,現在你想獲得矩形的latlng。

我們先去計算部分再到編程部分。假設是 -

中心點= E

左上方= A

右上方= B

右下= C

左下方= d

中點的AD = f

AB的中點= g

計算部

位置克參數 - lat-A.lat,長e.long

位置˚F參數 - lat-e.lat,長A.long

到g A的距離爲= A.distanceTo(克)

距離A到F是= A.distanceTo(F)

點B = 2AG從A

點C = 2AF從乙

點d = 2AF從A

編程部分

LatLng A = null, B = null, C = null, D = null, e = null, f = null, g = null,temp=null; 

e.latitude = your center latitude value; 
e.longitude = your center longitude value; 

A.latitude=your top left point latitude value; 
A.longitude=your top left point longitude value; 

f.latitude = e.latitude; 
f.longitude = A.longitude; 

g.latitude = A.latitude; 
g.longitude = e.longitude; 

double[] Ag = new double[1]; 
double[] Af = new double[1]; 

Location.distanceBetween(A.latitude, A.longitude, g.latitude, g.longitude, Ag); 
Location.distanceBetween(A.latitude, A.longitude, f.latitude, f.longitude, Af); 

temp=getDestinationPoint(A,90,(2*Ag)); 
B.latitude=temp.latitude; 
B.longitude=temp.longitude; 

temp=getDestinationPoint(B,180,(2*Af)); 
C.latitude=temp.latitude; 
C.longitude=temp.longitude; 

temp=getDestinationPoint(A,180,(2*Af)); 
D.latitude=temp.latitude; 
D.longitude=temp.longitude; 

private LatLng getDestinationPoint (LatLng source,double brng, double dist){ 
      dist = dist/6371; 
      brng = Math.toRadians(brng); 

      double lat1 = Math.toRadians(source.latitude), lon1 = Math.toRadians(source.longitude); 
      double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + 
        Math.cos(lat1) * Math.sin(dist) * Math.cos(brng)); 
      double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * 
          Math.cos(lat1), 
        Math.cos(dist) - Math.sin(lat1) * 
          Math.sin(lat2)); 
      if (Double.isNaN(lat2) || Double.isNaN(lon2)) { 
       return null; 
      } 
      return new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2)); 
     } 

說明

的點f和g是中期線AD和AB的點分別。我們可以通過改變A和e點的緯度和長度來獲得它。通過這兩個長度(Af和Ag),我們可以根據需要得到矩形的四個經緯度點。

謝謝

相關問題