Show Current Location and Update Location in MKMapView in Swift

You have to override CLLocationManager.didUpdateLocations (part of CLLocationManagerDelegate) to get notified when the location manager retrieves the current location: func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = locations.last{ let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) self.map.setRegion(region, animated: true) } } NOTE: … Read more

iOS6 MKMapView using a ton of memory, to the point of crashing the app, anyone else notice this?

After a lot of playing around and testing different ideas, some of which were mentioned here, the final solution that worked for me was as follows. Instead of creating new MKMapView’s as needed in the app, I added an mkMapView property to my AppDelegate and only created it when needed. Once it has been created, … Read more

Swift MKMapView Polygon Overlay glitching

It looks like you’re using an MKPolygon even though you’re only drawing a single point, with the same latitude and longitude. I think it would be better to use an MKCircle for a single point. func drawLocation(_ loc: CLLocation) { let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude) let circle = MKCircle(center: center, radius: 4) DispatchQueue.main.async(execute: … Read more

How to add a push pin to a MKMapView(IOS) when touching?

You can use a UILongPressGestureRecognizer for this. Wherever you create or initialize the mapview, first attach the recognizer to it: UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; lpgr.minimumPressDuration = 2.0; //user needs to press for 2 seconds [self.mapView addGestureRecognizer:lpgr]; [lpgr release]; Then in the gesture handler: – (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state != UIGestureRecognizerStateBegan) return; … Read more

Quickly adding single pin to MKMapView?

Instead of using the -mapView:viewForAnnotation: method, just put the code for an MKPointAnnotation into your -viewDidLoad method. It won’t animate the drop, but it is very easy. // Place a single pin MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; [annotation setCoordinate:centerCoordinate]; [annotation setTitle:@”Title”]; //You can set the subtitle too [self.mapView addAnnotation:annotation]; Swift version: let annotation = … Read more

Draw a circle of 1000m radius around users location in MKMapView

Try a custom overlay. Add this in viewDidLoad: MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000]; [map addOverlay:circle]; userLocation can be obtained by storing the MKUserLocationAnnotation as a property. Then, to actually draw the circle, put this in the map view’s delegate: – (MKOverlayRenderer *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay { MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithOverlay:overlay]; circleView.strokeColor = … Read more

MKMapView: Instead of Annotation Pin, a custom view

When you want to use your own image for an annotation view, you should create an MKAnnotationView instead of an MKPinAnnotationView. MKPinAnnotationView is a subclass of MKAnnotationView so it has an image property but it generally overrides that and draws a pin image (that’s what it’s for). So change the code to: -(MKAnnotationView *)mapView:(MKMapView *)mV … Read more

How do I zoom an MKMapView to the users current location without CLLocationManager?

You have to register for KVO notifications of userLocation.location property of MKMapView. To do this, put this code in viewDidLoad: of your ViewController or anywhere in the place where your map view is initialized. [self.mapView.userLocation addObserver:self forKeyPath:@”location” options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:NULL]; Then implement this method to receive KVO notifications – (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)