0
  • MKMapView
  • CoreLocation
  • Swift3.0

I have a requirement like this:

Creating an application in which I am getting Lat and Long for various location from webservice . Ploting these locations on map with Custom AnnotationView . Apart from this, I'm showing User Current Location with Custom Image.

Here everything working fine.

Now, when user moves then User Current Location Pin changes its position so there is an important requirement for this, I need to make this Current Location position in such a way that it stick in the bottom of map (just 100 pixel) above from bottom.

I did Some RND and find out few useful links:

But this not working anymore. Kindly suggest how can I set user Current Location pin in iOS map in such a way that it always at the bottom of Map (100 pixel above from Bottom)

Required Pin position: enter image description here

Currently it is showing in other position.

Note - **Dont want Google Map for such functionality.

Community
  • 1
  • 1
Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
  • I do not really understand your question, do you want to 1.have the pin to be visually at that location even when the user pans around or 2.pin the map to that spot when the user does an action or 3.have the map shift that the pin is at the location the screen? You might want to describe you problem clearer or add some images to show the result you want – Ben Ong Dec 09 '16 at 10:25
  • @Ben Ong i want to have the pin to be visually at that location even when the user pans around – Shobhakar Tiwari Dec 09 '16 at 10:33
  • @BenOng i updated question with screenshot , i need same pin position whenever user zoom and first time screen shows – Shobhakar Tiwari Dec 10 '16 at 09:36
  • @vadian kindly share your suggestion on this : http://stackoverflow.com/q/41057877/3400991 – Shobhakar Tiwari Dec 13 '16 at 14:17

2 Answers2

1

If you have been using something like this mapView.setUserTrackingMode(.Follow, animated: true) then user location will be always displayed in the center of the map, zoom/pan will be automatically adjusted whenever user location is updated.

You need to disable the UserTrackingMode and then manually adjust the map zoom/pan using func setVisibleMapRect(_ mapRect: MKMapRect, edgePadding insets: UIEdgeInsets, animated animate: Bool) each time whenever user location is changed.

I used following code in similar situation

let annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate);
var zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 1, 1);
annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 1, 1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
mapView.setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(40, 50, 160, 50), animated: true)

p.s. edgePadding is your friend in this case.

Zee
  • 1,865
  • 21
  • 42
  • so where i need to use this code , which delegate i mean – Shobhakar Tiwari Dec 09 '16 at 18:54
  • i updated question with screenshot , i need same pin position whenever user zoom and first time screen shows – Shobhakar Tiwari Dec 10 '16 at 09:35
  • 1
    I'd go with this answer, you can use these codes in func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) of the CLLocationMangerDelegate - probably your view controller or the superview of your mapView – Ben Ong Dec 12 '16 at 01:46
0

Here is the trick to use to position the map in such a way that UserLocation pin gets adjusted automatically :

func setUserLocationOnLowerPositiononMap(coordinate: CLLocationCoordinate2D) {
        var region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        region.center = coordinate
        self.map.setRegion(region, animated: true)
        //self.map.moveCenterByOffSet(offSet: CGPoint(x: 0, y: -130), coordinate: coordinate)
        self.map.moveCenterByOffSet(offSet: CGPoint(x: 0, y: SCREEN_HEIGHT - SCREEN_HEIGHT * 4/3 + 0 ), coordinate: coordinate)
    }
Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71