Note that there are some explanatory texts on larger screens.

plurals
  1. POAnnotations on MapView: Coordinates
    text
    copied!<p>I have two <code>NSString</code>s (address, and key) which contain the coordinates (longitude and latitude) in form of numbers (34,56789...):</p> <pre><code>NSString *key = [allKeys objectAtIndex:i]; NSObject *obj = [DictionaryMap objectForKey:key]; NSString *address = [NSString stringWithFormat:@"%@", obj]; CLLocationCoordinate2D anyLocation; anyLocation.latitude = [address doubleValue]; anyLocation.longitude = [key doubleValue]; MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init]; annotationPoint2.coordinate = anyLocation; annotationPoint2.title = @"Event"; annotationPoint2.subtitle = @"Microsoft's headquarters2"; [mapView addAnnotation:annotationPoint2]; </code></pre> <p>...But I can't understand why it doesn't plot in the same point as the coordinates written. I think this doesn't work:</p> <pre><code>[address doubleValue] </code></pre> <p>So I tried replacing it with:</p> <pre><code>location.latitude = NSNumber/NSString </code></pre> <p>but it gives an error.</p> <p>UPDATE:</p> <p>IN VIEW DID LOAD:</p> <pre><code> UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; [self.mapView addGestureRecognizer:longPressGesture]; [mapView.userLocation setTitle:@"I am here"]; </code></pre> <p>..then...</p> <pre><code>-(void)handleLongPressGesture:(UIGestureRecognizer*)sender { // This is important if you only want to receive one tap and hold event if (sender.state == UIGestureRecognizerStateEnded) { [self.mapView removeGestureRecognizer:sender]; } else { // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map CGPoint point = [sender locationInView:self.mapView]; CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView]; // Then all you have to do is create the annotation and add it to the map MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; annotationPoint.coordinate = locCoord; annotationPoint.title = @"Microsoft"; annotationPoint.subtitle = @"Microsoft's headquarters"; [mapView addAnnotation:annotationPoint]; NSString *latitude = [[NSString alloc] initWithFormat:@"%f",locCoord.latitude]; NSString *longitude = [[NSString alloc] initWithFormat:@"%f", locCoord.longitude]; NSLog(latitude); NSLog(longitude); [[NSUserDefaults standardUserDefaults]setObject:latitude forKey:@"FolderLatitude"]; [[NSUserDefaults standardUserDefaults]setObject:longitude forKey:@"FolderLongitude"]; } } </code></pre> <p>...I then save the coordinates in a JSON file and then read them from the file.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload