开发者

Get user current location on MKMapView

开发者 https://www.devze.com 2023-03-31 16:03 出处:网络
I am trying to get the user current latest location on the didUpdateToLocation:newLocation:fromLocation:oldLocation: method and store it in my ivar CLLocation *userCurrentLocation

I am trying to get the user current latest location on the didUpdateToLocation:newLocation:fromLocation:oldLocation: method and store it in my ivar CLLocation *userCurrentLocation

I get this error when I tried to read userCurrentLocation

-[NSCFNumber coordinate]: unrecognized selector sent to instance 0x586b220



- (void) locationManager:(CLLocationManager *) manager
     didUpdateToLocation:(CLLocation *) newLocation
            fromLocation:(CLLocation *) oldLocation {
            fromLocation:(CLLocation *) oldLocation {
if (oldLocation == nil) { // first time loading? 
        SVGeocoder *geocodeRequest = [[SVGeocoder alloc] 
                                      initWithCoordinate:CLLocationCoordinate2DMake(newLocation.coordinate.latitude,     newLocation.coordinate.longitude)];
         [geocodeRequest setDelegate:self];
        [geocodeRequest startAsynchronous]; // reverse geocoding to get annotation coordinates to be placed.
        [geocodeRequest release];
        isCurrentLocation = YES; //flag that next annotation placed is current location
    }
        userCurrentLocation = newLocation;
NSLog(@"didUpdateToLocation - Lat:%f Long:%f", userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude); // verified that userCurrentLocation latitude is correct at this point.
}

Toolbar button to show user current location

-(IBAction) showLocation:(id) sender {
    NSLog(@"Lat:%f Long:%f", userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude); // crashes upon calling this statement. Why?
    NSLog(@"Show Location Called");
    SVGeocoder *geocodeRequest = [[SVGeocoder alloc] 
                                  initWithCoordinate:CLLocationCoordinate2DMake(userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude)];
    [geocodeRequest setDelegate:self];
    [geocodeRequest startAsynchronous];
    [geocodeRequest release];

}

I did set the property and synthesize userCurrentLocation as such:

MapViewController.h

@interface MapViewController : UIViewController <CLLocationManagerDelegate,  MKMapViewDelegate> {
   CLLocation *userCurrentLocation;
}
@property (开发者_如何转开发nonatomic, retain) CLLocation *userCurrentLocation;
@end

MapViewController.m

@synthesize userCurrentLocation;

          /* EDITED */
- (void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
    userCurrentLocation = [[CLLocation alloc] init];
}


Your userCurrentLocation is autoreleased after the - (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation method finished.

Try use self.userCurrentLocation = newLocation; instead.


- (void)viewDidLoad {
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,46,320,370)];
    [mapView setDelegate:self];
    [mapView setShowsUserLocation:TRUE];    
    [self.view addSubview:mapView];
    [mapView setMapType:MKMapTypeStandard];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    if(locationManager.locationServicesEnabled)
    {
        [locationManager startUpdatingLocation];
    }
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    LatNLon = newLocation.coordinate;
    NSLog(@"%f   %f",LatNLon.latitude,LatNLon.longitude);
    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:LatNLon];
    [geocoder setDelegate:self];
    [geocoder start];
    isVisit=0;
    [locationManager stopUpdatingLocation];
}   


This is the code for UIPageControl in UIScrollView here we are using delegate of UIScrollView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int pageIndex=scrollView.contentOffset.x / scrollView.frame.size.width;
    self.pageCntrl.currentPage=pageIndex;
}


    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *currentUserLocation = [locations lastObject];
NSLog(@"lat%f - lon%f", currentUserLocation.coordinate.latitude, currentUserLocation.coordinate.longitude);

CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = currentUserLocation.coordinate.latitude;
zoomLocation.longitude= currentUserLocation.coordinate.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.2*METERS_PER_MILE, 0.2*METERS_PER_MILE);
[mapView setRegion:viewRegion animated:YES];
[locationManager stopUpdatingLocation];

}

0

精彩评论

暂无评论...
验证码 换一张
取 消