2011-08-03 2 views
1

どのようにして2点の間に地図を配置できますか?最初の座標と終点座標があり、2つのピンを表示したいと思います。ピンを配置することはできますが、マップの中心を設定する方法がわかりません。2点間でモノタッチマップを設定する方法

ポイントから正確な距離を計算し、その位置にマップを設定するには、数学を見つける必要がありますか?このための関数が組み込まれていますか?

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

答えて

1

2つの座標の中間点を計算するには、簡単な式が必要です。たとえば、(x1、y1)と(x2、y2)の2つの座標があるとします。

中点座標は、((x1 + x2)/ 2、(y1 + y2)/ 2)です。

したがって、たとえば、地図座標に、あなたは、次のスタート/エンドポイントを持っているとしましょう:

。 long:40、lat:39 b。 (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から継承するクラスです。 } } ``

関連する問題