Question

In my mapView application i am trying to make the annotation, Without adding annotation i am getting the mapView but when i try to add annotation, Only the annotation mark is visible the map becomes invisible.

Before adding Annotation:

Code:

 [super viewDidLoad];
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, elf.view.frame.size.height)];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeStandard;
mapView.delegate = self;
[self.view addSubview:mapView]; 

Image:

enter image description here
After Adding Annotation:

Code:

 [super viewDidLoad];
    mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
    mapView.showsUserLocation = YES;
    mapView.mapType = MKMapTypeStandard;
    mapView.delegate = self;
    [self.view addSubview:mapView]; 



    MKUserLocation *userLocation = mapView.userLocation;
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 20, 20);
    [mapView setRegion:region];

   MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
    annotation.coordinate = userLocation.location.coordinate;
    annotation.title = @"Here you r";
    annotation.subtitle = @"Pondy";
    [mapView addAnnotation:annotation];

Image:

enter image description here

I need the annotation appear in the map. Thanks in advance.

Was it helpful?

Solution 2

it add anotation of your current location...You put span 20, 20. That is very close so only it takes some time to load

    mapView1.showsUserLocation = YES;

    MKUserLocation *userLocation = mapView1.userLocation;
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 200, 200);
    [mapView1 setRegion:region];

MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
        annotation.coordinate = userLocation.location.coordinate;
        annotation.title = @"Title";
        annotation.subtitle = @"Sub Title";
        [mapView1 addAnnotation:annotation];

I hope it will be work for you

OTHER TIPS

Try changing the value of MKCoordinateRegionMakeWithDistance.

Blue screen usually means your current location is in the sea as discussed above. Please check the current location which you are getting is correct or not.

Usually for showing current location in MKMap we write map.showuserlocation=YES and it shows the bluew dot in the map view if you have to change the annotation pin in your map of the currentlocation you can go in view for annotation methiod and check for the userlocation class and hence change the pin of your userlocation annotation.

I think that at the time you add your annotation, the user location is still unknown:

MKUserLocation *userLocation = mapView.userLocation;
// At that time maybe the userLocation hasn't been retrieved through the GPS yet
...
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = userLocation.location.coordinate;
annotation.title = @"Here you r";
annotation.subtitle = @"Pondy";
[mapView addAnnotation:annotation];

In that case, the annotation position will be 0, 0 and as your map region is also based on the user location from the mapView, you'll end in the middle of the sea at position 0,0.

In order to monitor changes of the user location of the mapView, and then readjust your pin and / or the map region only when you're sure that the userLocation given by the mapView is correct, just implement the MKMapViewDelegate protocol for this selector :

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

then, in the body of your implementation, do what you did previously, but now, you can check if the user location is correct or not, and only adjust your map region and annotation once you're sure that the user location is OK.

The reason is location of your simulator is set to NONE.

Follow these steps, you will get your answer... `

          1) Open your simulator. 
          2) go to Debug menu
          3) Select Location.
          3) Select Custom Location. (OR Select location of Apple to skip step
          No 4.)
          4) Enter your Latitude and Longitude.
          5) Delete your app from simulator.
          6) Run your project again.

`

Enjoy....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top