2017-01-13 7 views

답변

1

코드 : http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/

extension MKMapView { 
    var MERCATOR_OFFSET : Double { 
     return 268435456.0 
    } 

    var MERCATOR_RADIUS : Double { 
     return 85445659.44705395 
    } 

    private func longitudeToPixelSpaceX(longitude: Double) -> Double { 
     return round(MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * M_PI/180.0) 
    } 

    private func latitudeToPixelSpaceY(latitude: Double) -> Double { 
     return round(MERCATOR_OFFSET - MERCATOR_RADIUS * log((1 + sin(latitude * M_PI/180.0))/(1 - sin(latitude * M_PI/180.0)))/2.0) 
    } 

    private func pixelSpaceXToLongitude(pixelX: Double) -> Double { 
     return ((round(pixelX) - MERCATOR_OFFSET)/MERCATOR_RADIUS) * 180.0/M_PI; 
    } 

    private func pixelSpaceYToLatitude(pixelY: Double) -> Double { 
     return (M_PI/2.0 - 2.0 * atan(exp((round(pixelY) - MERCATOR_OFFSET)/MERCATOR_RADIUS))) * 180.0/M_PI; 
    } 

    private func coordinateSpan(withMapView mapView: MKMapView, centerCoordinate: CLLocationCoordinate2D, zoomLevel: UInt) ->MKCoordinateSpan { 
     let centerPixelX = longitudeToPixelSpaceX(centerCoordinate.longitude) 
     let centerPixelY = latitudeToPixelSpaceY(centerCoordinate.latitude) 

     let zoomExponent = Double(20 - zoomLevel) 
     let zoomScale = pow(2.0, zoomExponent) 

     let mapSizeInPixels = mapView.bounds.size 
     let scaledMapWidth = Double(mapSizeInPixels.width) * zoomScale 
     let scaledMapHeight = Double(mapSizeInPixels.height) * zoomScale 

     let topLeftPixelX = centerPixelX - (scaledMapWidth/2); 
     let topLeftPixelY = centerPixelY - (scaledMapHeight/2); 

     // // find delta between left and right longitudes 
     let minLng = pixelSpaceXToLongitude(topLeftPixelX) 
     let maxLng = pixelSpaceXToLongitude(topLeftPixelX + scaledMapWidth) 
     let longitudeDelta = maxLng - minLng; 

     let minLat = pixelSpaceYToLatitude(topLeftPixelY) 
     let maxLat = pixelSpaceYToLatitude(topLeftPixelY + scaledMapHeight) 
     let latitudeDelta = -1 * (maxLat - minLat); 

     let span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta) 
     return span 
    } 

    func zoom(toCenterCoordinate centerCoordinate:CLLocationCoordinate2D ,zoomLevel: UInt) { 
     let zoomLevel = min(zoomLevel, 20) 
     let span = self.coordinateSpan(withMapView: self, centerCoordinate: centerCoordinate, zoomLevel: zoomLevel) 
     let region = MKCoordinateRegionMake(centerCoordinate, span) 
     self.setRegion(region, animated: true) 

    } 
} 
3

당신은 MKCoordinateRegion 객체를 만들고 있음을 설정할 것입니다 귀하의 MKMapView 개체의 영역과.

MKCoordinateRegion mapRegion; 
CLLocationCoordinate2D coordinate; 
coordinate.latitude = 0; 
coordinate.longitude = 0;  
mapRegion.center = coordinate; 
mapRegion.span.latitudeDelta = 0.2; 
mapRegion.span.longitudeDelta = 0.2; 

[mapView setRegion:mapRegion animated: YES]; 
1

MapKit 기능 didUpdateLocations에서 3.0

스위프트 :

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
      guard let location = locations.last as CLLocation? else { return } 

      let userCenter = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 
      // Does not have to be userCenter, could replace latitude: and longitude: with any value you would like to center in on 

      let region = MKCoordinateRegion(center: userCenter, span: MKCoordinateSpan(latitudeDelta: 180, longitudeDelta: 180)) 

      mkView.setRegion(region, animated: true) 

    } 

참고 : 당신은 그 수행의 중심을 위치 업데이트가있을 때마다 설정을 유지하지 않으려면 다음 :

 let userCenter = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 
     // Does not have to be userCenter, could replace latitude: and longitude: with any value you would like to center in on 

     let region = MKCoordinateRegion(center: userCenter, span: MKCoordinateSpan(latitudeDelta: 180, longitudeDelta: 180)) 

     mkView.setRegion(region, animated: true) 
1

H 너는 미리 정의 된 CLLocation에지도를 가운데 맞추기 위해 사용하는 방법이다. MKCoordinateRegion을 사용한다.

func centerMapOnLocation(_ location: CLLocation, mapView: MKMapView) { 
    let regionRadius: CLLocationDistance = 1000 
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 
     regionRadius * 2.0, regionRadius * 2.0) 
    mapView.setRegion(coordinateRegion, animated: true) 
}