0

I'm creating a button that will place a pin on users current location. But when ever I press it, instead is is placed in the middle of the global map, around Africa, I believe this is because in the original tutorial where I got this from, they created a void method with "didUpdateUserLocation" but I can figure a way to add this to the IBAction , here's what am working with. Has anyone ever had trouble with this?

This is the zooming in code but notice the didUpdateUserLocation down there vvvv

-(void)mapView: (MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
     MKCoordinate region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,   
     800,800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];
}

`

Now here is the IBAction that when tapped, drops pin around Africa or the center of the global map. I figured it's because it is missing the didUpdateUserLocation

-(IBAction)addAnnotation; 
{
   MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,   
     800,800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];

   MKPointAnnotation *point = [[MKPointAnnotation Alloc] init];
   point.coordinate = myLocation.coordinate;
   point.title = @"Test";
   point.subtitle = @"Text";
   [self.mapView addAnnotation:point];
}

So if anybody would know how to, what i figure is the solution, didUpdateUserLocation to the IBAction, please let me know, appreciate it!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What is the coordinate of `myLocation`? Might be around Africa.. – Martol1ni Jan 31 '14 at 10:42
  • it's a MKUserLocation, sorry forgot to mention it – Fernando Boza Jan 31 '14 at 10:52
  • The code you posted won't compile. Please copy+paste the _actual_ code. How and where is `myLocation` set? Is showsUserLocation set to YES on the map view? Does the map view show the blue dot? Is the map view's delegate set/connected? –  Jan 31 '14 at 13:12
  • hi Anna sorry I didnt post enough info, first time, myLocation is a MKUserLocation and in the header file. showUserLocation is check on via the interface builder, yes the blue dot is shown and and the delegate is connected. http://pastebin.com/embed_js.php?i=jm9kr7gk – Fernando Boza Jan 31 '14 at 13:38
  • You never set myLocation so it has coordinates 0,0 which is off the coast of Africa. Why do you even need your own variable? Just use mapView.userLocation. But check if mapView.userLocation.location is nil before using it (map view may not have obtained user location before you tap the button). Also make sure both the map view IBOutlet _and_ the delegate are connected (two things to connect). –  Feb 01 '14 at 01:28
  • the IBOutlet is connected but how would I connect the delegate ? do you mean via pick and drag? as for mapView.userLocation it doesn't seem to work. would that be a delegate issue ? – Fernando Boza Feb 02 '14 at 03:15

1 Answers1

0

Try this...

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation
{
    MKCoordinateRegion region;

    CLLocationCoordinate2D location;
    location.latitude = aUserLocation.coordinate.latitude;
    location.longitude = aUserLocation.coordinate.longitude;

    region.span = aMapView.region.span;
    region.center = location;
    [self.objMapView setRegion:region animated:FALSE];

    showCurrentLoc = FALSE;
}

-(IBAction)addAnnotation;
{
    MKCoordinateRegion region;
    region.span = self.mapView.region.span;
    region.center = location;
    [self.mapView setRegion:region animated:FALSE];

    MKPointAnnotation *point = [[MKPointAnnotation Alloc] init];
    point.coordinate = self.mapView.userLocation.coordinate;
    point.title = @"Test";
    point.subtitle = @"Text";
    [self.mapView addAnnotation:point];
}
Rajesh Choudhary
  • 1,340
  • 8
  • 12