set current location icon lower side in MKMapView

走远了吗. 提交于 2019-12-01 10:38:36

Don't set the userTrackingMode or the map's centerCoordinate.

Instead, you can try pointing the MKMapCamera to a coordinate slightly ahead of the user's in the direction they are heading. This will automatically put the user's location lower on the screen.

Calculating the coordinate "a short distance ahead" is not currently built into the iOS SDK so you'd have to calculate it manually.

One way to do it is using the method shown in:
Calculate new coordinate x meters and y degree away from one coordinate.

Using the coordinateFromCoord:atDistanceKm:atBearingDegrees: method from that answer, you could set the camera like this:

MKMapCamera *cam = [MKMapCamera camera];

CLLocationCoordinate2D coordinateAhead = 
    [self coordinateFromCoord:currentLocation.coordinate 
                 atDistanceKm:0.15 
             atBearingDegrees:currentLocation.course];
//adjust distance (0.15 km) as needed

cam.centerCoordinate = coordinateAhead;

cam.heading = currentLocation.course;

cam.pitch = 80; //adjust pitch as needed (0=look straight down)

cam.altitude = 100; //adjust as needed (meters)

[mapView setCamera:cam animated:YES];

Try this with the "City Bicycle Ride", "City Run", or "Freeway Drive" in the simulator.
You may need to adjust some of the numbers to get the perspective you want.

Setting layoutMargins on MKMapView works just fine.

According to docs,

The default spacing to use when laying out content in the view.

In iOS 11 and later, use the directionalLayoutMargins property to specify layout margins instead of this property.

To offset camera's centering point to be centered in bottom half of the MKMapView, just set UIEdgeInsets.top to half of MKMapView's height.

class MapViewController: UIViewController {
@IBOutlet var mapView: MKMapView!

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let layoutMargins = UIEdgeInsets(top: self.mapView.bounds.size.height / 2, left: 0, bottom: 0, right: 0)
    self.mapView.layoutMargins = layoutMargins
}

A simple workaround that should work is to make the MKMapView frame bigger than the screen of the device. Something like:

MapViewHeight = (WindowHeight - desiredOffsetFromBottom)*2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!