简体   繁体   中英

User current Location on a mapkit in xcode

I need a good tutorial in which user's current location (latitude,longitude,city,state,country) is found and updated from time to time as the location changes and displayed on the map kit with the blue icon zooming.

I did it by placing an MKMapView on view.xib and it shows current location of user (default on simulator :SanFransisco) with blue dot zooming only for the first time. But when I run the app next time it is not showing any blue dot zooming. Should I write any code? Till now I didn't write any code. Just placed a mapkit with Show UserLocation checked in xib. How can I get a blue dot ?

I also need to find nearby doctors from the userlocation and display in the same map with red coloured markers pointing.

Gone through google but confused a lot. Please suggest to me some good tutorials in this regard.

EDIT:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mapView.delegate = self;
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.mapView setShowsUserLocation:YES];
    [locationManager startUpdatingLocation];
    [self queryGooglePlaces:@"doctor"];
}

-(void) queryGooglePlaces: (NSString *) googleType {

    // https://developers.google.com/maps/documentation/places/#Authentication
    NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=300&types=doctor&sensor=true&key=%@",  coord.latitude, coord.longitude,kGOOGLE_API_KEY];


    NSURL *googleRequestURL=[NSURL URLWithString:url];

        dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

//Here I'm getting array data as null because latitude and longitude are passed as 0.000 ..

How can I get both of them on viewDidLoad?

if you want to find the near doctor address you need to use "GOOGLE PLACE API"

//For user current location

CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

Check this link its good example of google place api

Add this Delegate method of ccl location manager

.h file

double currentLatitude;
double currentLongitude;

  - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation
    {

        currentLatitude = newLocation.coordinate.latitude;

        currentLongitude = newLocation.coordinate.longitude;

        [self  queryGooglePlaces:somestring];
    }

-(void) queryGooglePlaces: (NSString *) googleType {

    // https://developers.google.com/maps/documentation/places/#Authentication
    NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=300&types=doctor&sensor=true&key=%@",  currentLatitude , currentLongitude,kGOOGLE_API_KEY];


    NSURL *googleRequestURL=[NSURL URLWithString:url];

        dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

pass the current latitude and longitude in your -(void) queryGooglePlaces: (NSString *) googleType { } method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM