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

13,638

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];
Share:
13,638

Related videos on Youtube

chandru
Author by

chandru

Greetings..! I am a graduate in engineering, have a good knowledge in C and CPP, but totally new to Objective C. I am currently undergoing training as a iPhone App Developer.

Updated on September 16, 2022

Comments

  • chandru
    chandru over 1 year

    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?

  • chandru
    chandru over 10 years
    Three errors are displayed, use of undeclared identifier "Annotation".
  • iGagan Kumar
    iGagan Kumar over 10 years
    Its MKAnnotation not Annotation
  • Sukesh
    Sukesh over 10 years
    THE_SPAN 0.01f In the place of latitude and longitude give your value...
  • chandru
    chandru over 10 years
    Ya,thank u, this works perfect, can we use any icons(own Pictures) other than annotation.
  • Bhumeshwer katre
    Bhumeshwer katre over 10 years
    You can use -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {} this method to customize annotation.
  • chandru
    chandru over 10 years
    No, it is not working, any way thank u, but the first answer worked.
  • chandru
    chandru over 10 years
    Wat is that above comment
  • Sukesh
    Sukesh over 10 years
    In the above coding in the place of THE_SPAN give value 0.01f,and in the place of latitude and longitude give your latitude and longitude values..
  • Bhumeshwer katre
    Bhumeshwer katre over 10 years
    You can get idea through this link blog.asolutions.com/2010/09/…
  • chandru
    chandru over 10 years
    k, wat is that MapViewC
  • Sukesh
    Sukesh over 10 years
    you would have declared the map view in .h file. Use the same name here and try.
  • chandru
    chandru over 10 years
    Ya, thank u, can we plot our own pictures instead of annotation
  • Sukesh
    Sukesh over 10 years