2012-12-25 45 views
1

我使用此代碼來獲取當前位置想要使用此信息共享像whatsapp。在共享位置按鈕點擊我想向其他用戶發送經度和緯度。但其顯示完全不同的位置需要有關MKMapItem的幫助

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]; 
    } 

,並有可能像WhatsApp的顯示附近的街道或地方命名共享。

回答

0

您可以使用CLLocationManagerDelegate更新位置並將其存儲在全局變量中。

請初始化的LocationManager作爲

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

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

//手柄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:@", "]]; 
     } 

    }]; 
    }