-1

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.

  • 2
    Looks like coordinates of added annotation are in the sea – nerowolfe Oct 25 '13 at 10:36
  • 1
    Yeah i guess the same. but how to make it appear in my current area? when i try scrolling it it keeps on scrolling with blue screen. – Siddarth Hailstorm Oct 25 '13 at 10:38
  • 2
    Change location for Simulator in Debug/Location – nerowolfe Oct 25 '13 at 10:40
  • And also, don't add user location to map by yourself. Map will make it for you. Just use method - (MKAnnotationView *)mv:(MKMapView *)mapView viewForAnnotation:(id )annotation and inside it if([annotation isKindOfClass:[MKUserLocation class]]) return nil; – nerowolfe Oct 25 '13 at 10:43
  • Any time. You are welcome – nerowolfe Oct 25 '13 at 11:12
  • This has been asked and answered many times and the question (as-is) is an unnecessary duplicate. You cannot assume that mapView.userLocation will contain a valid coordinate _immediately_ after setting showsUserLocation = YES. When the map view has a user location update, it will call the didUpdateUserLocation delegate method. Anyway, if you are setting showsUserLocation=YES, you don't need to add your own annotation since map will show blue dot for you. –  Oct 28 '13 at 13:17

5 Answers5

2

Try changing the value of MKCoordinateRegionMakeWithDistance.

1

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....

Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76
1

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.

creative_rd
  • 695
  • 6
  • 13
1

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

TamilKing
  • 1,643
  • 1
  • 12
  • 23
  • see his see his question first. He had used the same code. Dont repeat same code –  Oct 25 '13 at 11:58
  • @user2918674:he put span (userLocation.location.coordinate, 20, 20). His code correct he put span very close. It can't show that much close that is his problem.. – TamilKing Oct 25 '13 at 12:03
1

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.

yonel
  • 7,855
  • 2
  • 44
  • 51
  • Your answer is the correct one. It seems this question was simply a rep game of some sort. –  Oct 29 '13 at 13:35