38

外部サーバーへのクエリをセットアップするために、作成中の iPhone アプリで現在のマップ ビューの境界を取得したいと考えています。UIView は境界に応答する必要がありますが、MKMapView は応答しないようです。地域を設定してマップをズームした後、境界を取得しようとします。私は、マップの SE と NW のコーナーを表す CGPoints を取得しようとする最初のステップで立ち往生しています。その後、私は使用するつもりでした:

- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view

ポイントをマップ座標に変換します。でもそこまでたどり着けない…

//Recenter and zoom map in on search location
MKCoordinateRegion region =  {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = mySearchLocation.searchLocation.coordinate;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];


//After the new search location has been added to the map, and the map zoomed, we need to update the search bounds
//First we need to calculate the corners of the map
CGPoint se = CGPointMake(self.mapView.bounds.origin.x, mapView.bounds.origin.y);
CGPoint nw = CGPointMake((self.mapView.bounds.origin.x + mapView.bounds.size.width), (mapView.bounds.origin.y + mapView.bounds.size.height));
NSLog(@"points are: se %@, nw %@", se, nw);

コードは警告なしでコンパイルされますが、se と nw は両方とも null です。self.mapView.bounds.origin.x を見ると、変数は 0 です。NSLog を直接 self.mapView.bounds.size.width にしようとすると、「Program received signal: “EXC_BAD_ACCESS”.」というメッセージが表示されます。これは NSLog から来ているようです。

MKMapView の可視領域から南東の角と北西の角 (マップ座標) を取得する適切な方法を知っている人はいますか?

編集:ここで何かを尋ねると、すぐに答えが返ってくるようです。@f の代わりに %@ を使用して、そこにエラーをスローしていた NSLog の各変数を出力していました。また、MKMapview の annotationVisibleRect プロパティも発見しました。ですが、annotationVisibleRect は親ビューの座標に基づいているようです。

4

10 に答える 10

82

さて、私は自分の質問に正式に回答しましたが、どこにも見つからなかったので、ここに回答を投稿します。

//To calculate the search bounds...
//First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));

//Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord;
neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];

CLLocationCoordinate2D swCoord;
swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];

スイフト5

public extension MKMapView {

  var newBounds: MapBounds {
    let originPoint = CGPoint(x: bounds.origin.x + bounds.size.width, y: bounds.origin.y)
    let rightBottomPoint = CGPoint(x: bounds.origin.x, y: bounds.origin.y + bounds.size.height)

    let originCoordinates = convert(originPoint, toCoordinateFrom: self)
    let rightBottomCoordinates = convert(rightBottomPoint, toCoordinateFrom: self)

    return MapBounds(
      firstBound: CLLocation(latitude: originCoordinates.latitude, longitude: originCoordinates.longitude),
      secondBound: CLLocation(latitude: rightBottomCoordinates.latitude, longitude: rightBottomCoordinates.longitude)
    )
  }
}

public struct MapBounds {
  let firstBound: CLLocation
  let secondBound: CLLocation
}

使用法

self.mapView.newBounds
于 2010-01-17T19:14:16.617 に答える
43

もう 1 つのオプションは、MKMapView インスタンスで visibleMapRect プロパティを使用し、MKCoordinateForMapPoint() を使用して緯度/経度に変換することです。

MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint neMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), mRect.origin.y);
MKMapPoint swMapPoint = MKMapPointMake(mRect.origin.x, MKMapRectGetMaxY(mRect));
CLLocationCoordinate2D neCoord = MKCoordinateForMapPoint(neMapPoint);
CLLocationCoordinate2D swCoord = MKCoordinateForMapPoint(swMapPoint);

スイフト5

let rect = visibleMapRect
let neMapPoint = MKMapPoint(x: rect.maxX, y: rect.origin.y)
let swMapPoint = MKMapPoint(x: rect.origin.x, y: rect.maxY)

let neCoordinate = neMapPoint.coordinate
let swCoordinate = swMapPoint.coordinate
于 2010-12-06T05:04:46.330 に答える
15

すぐに離れて...(@deadroxyの回答に基づく...)

typealias Edges = (ne: CLLocationCoordinate2D, sw: CLLocationCoordinate2D)

extension MKMapView {
    func edgePoints() -> Edges {
          let nePoint = CGPoint(x: self.bounds.maxX, y: self.bounds.origin.y)
    let swPoint = CGPoint(x: self.bounds.minX, y: self.bounds.maxY)
    
    let neCoord = self.convert(nePoint, toCoordinateFrom: self)
    let swCoord = self.convert(swPoint, toCoordinateFrom: self)
    
    return (ne: neCoord, sw: swCoord)
    }
}
于 2015-02-23T21:30:05.753 に答える
2

これを Parse GeoBox クエリで動作させることができました。

//Calculate the corners of the map to get the points
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + self.mapView.bounds.size.width, self.mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x),(self.mapView.bounds.origin.y+ self.mapView.bounds.size.height));

//Transform points into lat/long values
CLLocationCoordinate2D NECoordinate = [self.mapView convertPoint:nePoint toCoordinateFromView:self.mapView];
CLLocationCoordinate2D SWCoordinate = [self.mapView convertPoint:swPoint toCoordinateFromView:self.mapView];

//Convert to Parse GeoPoints
PFGeoPoint *Southwest = [PFGeoPoint geoPointWithLatitude:SWCoordinate.latitude longitude:SWCoordinate.longitude];
PFGeoPoint *Northeast = [PFGeoPoint geoPointWithLatitude:NECoordinate.latitude longitude:NECoordinate.longitude];
于 2014-12-19T14:39:29.010 に答える