How to give a location coordinate to plot a point in a map in ios using storyboard?

chandru picture chandru · Nov 20, 2013 · Viewed 13.6k times · Source

I am creating a map view in a view controller, using storyboard.

When I use the following code.

-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationDistance distance = 1000;   
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,
                               distance, 
                               distance);
    MKCoordinateRegion adjusted_region = [self.mapView regionThatFits:region];
    [self.mapView setRegion:adjusted_region animated:YES];
}

A point is plotted in San Francisco, CA, United States. The userLocation coordinates are the predefined value in MapKit.h framework. Now I create a

-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationDistance distance = 1000;
    CLLocationCoordinate2D myCoordinate;
    myCoordinate.latitude = 13.04016;
    myCoordinate.longitude = 80.243044;
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(myCoordinate, 
                                                                   distance, 
                                                                   distance);
    MKCoordinateRegion adjusted_region = [self.mapView regionThatFits:region];
    [self.mapView setRegion:adjusted_region animated:YES];
}

Here, the region is displayed having the coordinate in the center. But, no point is plotted at the coordinate position.

How to plot a point or annotation in that coordinate location?

Answer

Bhumeshwer katre picture Bhumeshwer katre · Nov 20, 2013

Try this code inside of didUpdateUserLocation method

    MKPointAnnotation*    annotation = [[MKPointAnnotation alloc] init];
    CLLocationCoordinate2D myCoordinate;
    myCoordinate.latitude=13.04016;
    myCoordinate.longitude=80.243044;
    annotation.coordinate = myCoordinate;
    [self.mapView addAnnotation:annotation];