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

3

8

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?

Cobbie answered 20/11, 2013 at 6:10 Comment(0)
S
11

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];
Sifuentes answered 20/11, 2013 at 6:15 Comment(3)
Ya,thank u, this works perfect, can we use any icons(own Pictures) other than annotation.Cobbie
You can use -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {} this method to customize annotation.Sifuentes
You can get idea through this link blog.asolutions.com/2010/09/…Sifuentes
T
0

Add this code in didUpdateUserLocation

MKAnnotation *annotation = [[MKAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
[myMap addAnnotation:annotation];
Thermoelectricity answered 20/11, 2013 at 6:14 Comment(3)
Three errors are displayed, use of undeclared identifier "Annotation".Cobbie
Its MKAnnotation not AnnotationThermoelectricity
No, it is not working, any way thank u, but the first answer worked.Cobbie
R
0

Try This.. //MAP VIEW Point

MKCoordinateRegion myRegion;

//Center
CLLocationCoordinate2D center;
center.latitude=latitude;
center.longitude=longitude;

//Span
MKCoordinateSpan span;
span.latitudeDelta=THE_SPAN;
span.longitudeDelta=THE_SPAN;

myRegion.center=center;
myRegion.span=span;

//Set our mapView
[MapViewC setRegion:myRegion animated:YES];

//Annotation

//1.create coordinate for use with the annotation
CLLocationCoordinate2D wimbLocation;
wimbLocation.latitude=latitude;
wimbLocation.longitude=longitude;

Annotation * myAnnotation= [Annotation alloc];

myAnnotation.coordinate=wimbLocation;
Radford answered 20/11, 2013 at 6:19 Comment(7)
THE_SPAN 0.01f In the place of latitude and longitude give your value...Radford
Wat is that above commentCobbie
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..Radford
k, wat is that MapViewCCobbie
you would have declared the map view in .h file. Use the same name here and try.Radford
Ya, thank u, can we plot our own pictures instead of annotationCobbie
check it here #9815488Radford

© 2022 - 2024 — McMap. All rights reserved.