Obtendo os limites de um MKMapView

Para configurar uma consulta para um servidor externo, quero obter os limites da visualização de mapa atual em um aplicativo do iPhone que estou criando. O UIView deve responder aos limites, mas parece que o MKMapView não. Depois de definir uma região e zoom no mapa eu tento obter os limites. Estou preso no primeiro passo que é tentar obter os CGPoints que representam os cantos SE e NW do mapa. Depois disso eu ia usar:

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

Transformar os pontos em coordenadas do mapa. Mas eu não posso nem chegar tão longe …

 //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); 

O código compila sem avisos, no entanto se e nw são ambos nulos. Olhando para self.mapView.bounds.origin.x, a variável é 0. Tentando fazer o NSLog diretamente self.mapView.bounds.size.width me fornece um “Program received signal:” EXC_BAD_ACCESS “.” que parece vir do NSLog.

Alguém sabe o caminho certo para obter o canto sudeste e canto noroeste (em coordenadas do mapa) da área visível de um MKMapView?

EDIT: parece sempre que você pediu algo aqui a resposta vem a você logo depois. Eu estava usando% @ em vez de @f para imprimir cada variável no NSLog que estava lançando erros lá. Eu também descobri a propriedade annotationVisibleRect de MKMapview. No entanto, parece que o annotationVisibleRect é baseado nas coordenadas da vista pai.

Ok eu oficialmente respondi a minha própria pergunta, mas desde que eu não encontrei em qualquer lugar antes de postar a resposta aqui:

 //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]; 

Outra opção é usar a propriedade visibleMapRect na sua instância MKMapView e usar MKCoordinateForMapPoint () para converter para o lat / lon.

 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); 

Swift away … (Baseado na resposta do @ 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.convertPoint(nePoint, toCoordinateFromView: self) let swCoord = self.convertPoint(swPoint, toCoordinateFromView: self) return (ne: neCoord, sw: swCoord) } } 

Este http://wiki.openstreetmap.org/wiki/Bounding_Box é um documento para checkbox delimitadora

 bbox = left,bottom,right,top bbox = min Longitude , min Latitude , max Longitude , max Latitude 

insira a descrição da imagem aqui

Você pode ter uma estrutura BoundingBox que represente

 struct BoundingBox { let min: CLLocationCoordinate2D let max: CLLocationCoordinate2D init(rect: MKMapRect) { let bottomLeft = MKMapPointMake(rect.origin.x, MKMapRectGetMaxY(rect)) let topRight = MKMapPointMake(MKMapRectGetMaxX(rect), rect.origin.y) min = MKCoordinateForMapPoint(bottomLeft) max = MKCoordinateForMapPoint(topRight) } var points: [CLLocationDegrees] { return [ min.latitude, min.longitude, max.latitude max.longitude, ] } } 

O visibleMapRect é o mesmo que region.span

 let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: 320, height: 640)) XCTAssertEqual(mapView.userLocation.coordinate.latitude, 0) XCTAssertEqual(mapView.userLocation.coordinate.longitude, 0) let boundingBox = BoundingBox(rect: mapView.visibleMapRect) XCTAssertEqual(boundingBox.max.longitude-boundingBox.min.longitude, mapView.region.span.longitudeDelta) XCTAssertEqual(boundingBox.max.latitude-boundingBox.min.latitude, mapView.region.span.latitudeDelta) 

Consegui fazer isso funcionar com a consulta 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]; 

este site resolve o problema. http://www.softwarepassion.com/how-to-get-coographic-coordinates-of-the-visible-mkmapview-area-in-ios/

 MKMapRect mRect = self.mapView.visibleMapRect; -(CLLocationCoordinate2D)getNECoordinate:(MKMapRect)mRect{ return [self getCoordinateFromMapRectanglePoint:MKMapRectGetMaxX(mRect) y:mRect.origin.y]; } -(CLLocationCoordinate2D)getNWCoordinate:(MKMapRect)mRect{ return [self getCoordinateFromMapRectanglePoint:MKMapRectGetMinX(mRect) y:mRect.origin.y]; } -(CLLocationCoordinate2D)getSECoordinate:(MKMapRect)mRect{ return [self getCoordinateFromMapRectanglePoint:MKMapRectGetMaxX(mRect) y:MKMapRectGetMaxY(mRect)]; } -(CLLocationCoordinate2D)getSWCoordinate:(MKMapRect)mRect{ return [self getCoordinateFromMapRectanglePoint:mRect.origin.xy:MKMapRectGetMaxY(mRect)]; } -(CLLocationCoordinate2D)getCoordinateFromMapRectanglePoint:(double)xy:(double)y{ MKMapPoint swMapPoint = MKMapPointMake(x, y); return MKCoordinateForMapPoint(swMapPoint); } -(NSArray *)getBoundingBox:(MKMapRect)mRect{ CLLocationCoordinate2D bottomLeft = [self getSWCoordinate:mRect]; CLLocationCoordinate2D topRight = [self getNECoordinate:mRect]; return @[[NSNumber numberWithDouble:bottomLeft.latitude ], [NSNumber numberWithDouble:bottomLeft.longitude], [NSNumber numberWithDouble:topRight.latitude], [NSNumber numberWithDouble:topRight.longitude]]; } 

Este código funciona com o mapa girando como 90/180 graus. set mapView.pitchEnabled = NÃO; por menos bugs.

 CLLocationDirection heading = mapView.camera.heading; float mapWidth = mapView.frame.size.width; float mapHeight = mapView.frame.size.height; float neX = mapWidth; float neY = 0.0; float swX = 0.0; float swY = mapHeight; if (heading >= 0 && heading <= 90) { //println("Q1") float ratio = heading / 90; neX = (1-ratio) * mapWidth; swX = (mapWidth*ratio); } else if (heading >= 90 && heading <= 180) { //println("Q2") float ratio = (heading - 90) / 90; neX = 0; neY = (mapHeight*ratio); swY = (1-ratio) * mapHeight; swX = mapWidth; } else if (heading >= 180 && heading <= 270) { //println("Q3") float ratio = (heading - 180) / 90; neX = mapWidth*ratio; neY = mapHeight; swX = (1-ratio) * mapWidth; swY = 0; } else if (heading >= 270 && heading <= 360) { //println("Q4"); float ratio = (heading - 270) / 90; neX = mapWidth; neY = (1-ratio) * mapHeight; swY = ratio * mapHeight; } CGPoint swPoint = CGPointMake(swX, swY); CGPoint nePoint = CGPointMake(neX, neY); CLLocationCoordinate2D swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView]; CLLocationCoordinate2D neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView]; 

Eu estava tendo alguns problemas com algumas das outras respostas para mapas que foram girados com dois dedos. Este código funcionou para mim:

 MKMapRect rect = self.mapView.visibleMapRect; CLLocationCoordinate2D northeast = MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMaxX(rect),rect.origin.y)); CLLocationCoordinate2D southwest = MKCoordinateForMapPoint(MKMapPointMake(rect.origin.x ,MKMapRectGetMaxY(rect))); 

Minha resposta é derivada da resposta de and 保 and e o site associado insere a descrição do link aqui . É simplificado também 3 linhas para cantos sul oeste e nordeste.

Intereting Posts