2011-08-03 15 views
1

如何在兩點之間居中地圖?有點像本地地圖應用程序在位置A和位置B之間生成方向時的情況。我有一個起始座標和一個結束座標,我想顯示兩個引腳。我可以將引腳放置到位,但我不知道如何設置地圖的中心。如何設置兩點之間的Monotouch地圖

我是否需要找到數學計算從點的確切距離並將地圖設置爲該位置?有沒有內置函數?

this.currentMapView.SetCenterCoordinate(annotation.Coordinate,true);

回答

1

計算兩個座標之間的中點需要一個簡單的公式。例如,假設您有兩個座標:(x1,y1)和(x2,y2)。 ((x1 + x2)/ 2,(y1 + y2)/ 2)。

因此,例如,在地圖座標,讓我們假設你有以下的開始/結束點:

一個。長:40,緯度:39 b。 long:41,lat:38

它們的中點座標是:((40 + 41)/ 2,(39 + 38)/ 2)=(40.5,38.5) 因此,您將地圖視圖的中心座標設置爲這個公式的結果。

我不知道用於計算此功能的內置函數。

1

來自http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/

BasicMapAnnotation是繼承類從MKAnnotation

private void GetRegion(MKMapView mapView) 
     { 
      var userWasVisible = mapView.ShowsUserLocation; 
      mapView.ShowsUserLocation = false; // ignoring the blue blip 
// start with the widest possible viewport 
      var tl = new CLLocationCoordinate2D(-90, 180); // top left 
      var br = new CLLocationCoordinate2D(90, -180); // bottom right 
      foreach (var an in mapView.Annotations) 
      { 
       // narrow the viewport bit-by-bit 
       CLLocationCoordinate2D coordinate = ((BasicMapAnnotation) an).Coordinate; 
       tl.Longitude = Math.Min(tl.Longitude, coordinate.Longitude); 
       tl.Latitude = Math.Max(tl.Latitude, coordinate.Latitude); 
       br.Longitude = Math.Max(br.Longitude, coordinate.Longitude); 
       br.Latitude = Math.Min(br.Latitude, coordinate.Latitude); 
      } 
      var center = new CLLocationCoordinate2D 
          { 
           // divide the range by two to get the center 
           Latitude = tl.Latitude - (tl.Latitude - br.Latitude)*0.5 
           , 
           Longitude = tl.Longitude + (br.Longitude - tl.Longitude)*0.5 
          }; 
      var span = new MKCoordinateSpan 
          { 
           // calculate the span, with 20% margin so pins aren’t on the edge 
           LatitudeDelta = Math.Abs(tl.Latitude - br.Latitude)*1.2 
           , 
           LongitudeDelta = Math.Abs(br.Longitude - tl.Longitude)*1.2 
          }; 
      var region = new MKCoordinateRegion {Center = center, Span = span}; 
      region = mapView.RegionThatFits(region); // adjusts zoom level too 
      mapView.SetRegion(region, true); // animated transition 
      mapView.ShowsUserLocation = 

userWasVisible; } }``

相關問題