0
votes
  • 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.

2
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
@Ben Ong i want to have the pin to be visually at that location even when the user pans around - Shobhakar Tiwari
@BenOng i updated question with screenshot , i need same pin position whenever user zoom and first time screen shows - Shobhakar Tiwari
@vadian kindly share your suggestion on this : stackoverflow.com/q/41057877/3400991 - Shobhakar Tiwari

2 Answers

1
votes

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.

0
votes

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