Abdelrahman Manna
Abdelrahman Manna

Reputation: 73

showing the locations and finding the nearest spot in ios

I want to display my spots NSArray *chSpot = [parser parseArray:responseObject]; which receives 3 objects , I want to show each location regarding to its lat and long enter image description here

and find the nearest spot according to my location...I am using this code:

         NSArray *chSpot = [parser parseArray:responseObject];

// GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[locLat doubleValue] // longitude:[locLong doubleValue] // zoom:4];

     mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:nil];
     mapView_.myLocationEnabled = YES;
     mapView_.settings.myLocationButton = YES;


     for (int i=0; i<chSpot.count; i++)
     {

         ChargingSpots *spot = [chSpot objectAtIndex:i];
         NSString *locLat = spot.LocationLat;
         NSString *locLong = spot.LocationLong;


         // Creates a marker in the center of the map.
         GMSMarker *marker = [[GMSMarker alloc] init];
         marker.position = CLLocationCoordinate2DMake([locLat doubleValue], [locLong doubleValue]);
         marker.title = @"Amman";
         marker.snippet = @"Jordan";

         mapView_.delegate=self;
         marker.appearAnimation = kGMSMarkerAnimationPop;
         marker.icon = [UIImage imageNamed:@"car"];
         marker.map = mapView_;

     }

     //[mapView_ setSelectedMarker:marker];

     self.view = mapView_;

//
//
// CLLocation *locationA = [[CLLocation alloc] initWithLatitude:[locLat doubleValue] longitude:[locLong doubleValue]]; //
// CLLocation *locationB = [[CLLocation alloc] initWithLatitude:[locLat doubleValue] longitude:[locLong doubleValue]]; //
// CLLocationDistance distance = [ locationA distanceFromLocation:locationB]; //
// NSLog (@"%f",distance);

     self.locationManager = [[CLLocationManager alloc] init];
     self.locationManager.distanceFilter = kCLDistanceFilterNone;
     self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

     [self.locationManager startUpdatingLocation];

Upvotes: 2

Views: 481

Answers (1)

Mindaugas
Mindaugas

Reputation: 1735

In your locationManager's delegate's method locationManager:didUpdateLocations: you can get your current location and it provides the method to calculate distanceFromLocation:(const CLLocation *)location

CLLocationDistance distA = [currentLocation distanceFromLocation: locationA]; CLLocationDistance distB = [currentLocation distanceFromLocation: locationB]; //etc

Upvotes: 1

Related Questions