1

I am using this code to get current location want to use this info for sharing like whatsapp. On share location button tap I want to send latitude and longitude to other user. But its showing completely different location

CLLocationManager *lm = [[CLLocationManager alloc] init];
    lm.delegate = self;
    lm.desiredAccuracy = kCLLocationAccuracyBest;
    lm.distanceFilter = kCLDistanceFilterNone;
    [lm startUpdatingLocation];

    CLLocation *location = [lm location];

    CLLocationCoordinate2D coord = [location coordinate] ;

    Class mapItemClass = [MKMapItem class];
    if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
    {
        // Create an MKMapItem to pass to the Maps app
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(coord.longitude, coord.latitude);
        MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
        MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
        [mapItem setName:@"My Place"];
        // Pass the map item to the Maps app
        [mapItem openInMapsWithLaunchOptions:nil];
    }

and is it possible like whatsapp to show nearby street or places name to share.

S.J
  • 3,063
  • 3
  • 33
  • 66

1 Answers1

0

You can update the location using CLLocationManagerDelegate and store it in a global variable.

Initialise the locationManager as

 _locationManager = [[CLLocationManager alloc] init];
     _locationManager.delegate = _locationObjVC;
     locationManager.desiredAccuracy = kCLLocationAccuracyBest;

 [_locationManager startUpdatingLocation]; // start updating it..

// handle delgate

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
 CLLocation *location = [[CLLocation alloc] initWithLatitude:_userAnnotationPoint.coordinate.latitude longitude:_userAnnotationPoint.coordinate.longitude];
    [_geocoder reverseGeocodeLocation:location completionHandler:
     ^(NSArray *placemarks, NSError *error)
     {
         NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
         if (error){
             NSLog(@"Geocode failed with error: %@", error);
             //[self displayError:error];
             _userAnnotationPoint.subtitle = [NSString stringWithFormat:@"Lati:%f Long:%f",_userAnnotationPoint.coordinate.latitude,_userAnnotationPoint.coordinate.longitude];
         }
         else
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];

             NSLog(@"Received placemarks: %@", [NSString stringWithFormat:@"%@",[[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]]);
             //            clickedAnnotationPoint.subtitle = [NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ %@",
             //            placemark.ISOcountryCode,placemark.country,placemark.postalCode,placemark.administrativeArea,          placemark.subAdministrativeArea,placemark.locality,placemark.subLocality,placemark.thoroughfare,           placemark.subThoroughfare];
             _userAnnotationPoint.subtitle = [NSString stringWithFormat:@"%@",[[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]];
         }

     }];
    }
HDdeveloper
  • 4,396
  • 6
  • 40
  • 65