set current location icon lower side in MKMapView

2020-03-26 04:58发布

I want to show my current location lower in the map (iOS 6 and iOS 7) as per below screen shot to user can see further view [google default app with google map].

enter image description here

Right now, the cursor that shows center in the view as per below image [my app with apple map] . enter image description here

Therefore the largest part of the screen is used to display what's behind, while it cannot look forward very far.

In the first image and second image, I compare to Google Navigation, which shows the current position much lower in the screen, for about the same rotation angle. I've added some arrows to show what I'm talking about.

i tried below code for set center because i cannot find to set lower.

mapView.userTrackingMode = MKUserTrackingModeFollow;

and also try below method

[mapView setCenterCoordinate:currentLocation.coordinate animated:YES];

3条回答
男人必须洒脱
2楼-- · 2020-03-26 05:04

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
查看更多
Deceive 欺骗
3楼-- · 2020-03-26 05:12

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
}
查看更多
三岁会撩人
4楼-- · 2020-03-26 05:17

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.

查看更多
登录 后发表回答