Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5957961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:28:31+00:00 2026-05-22T18:28:31+00:00

Hi I have an array of mapAnnotations in my app. Each annotation object has

  • 0

Hi I have an array of mapAnnotations in my app. Each annotation object has an address and a coordinate. I am using my location manager to find out the user’s current location which is a CLLocationDistance object. Then I am trying to iterate over the mapAnnotations array and comparing the user’s location with the location coordinates of each annotation to find the one thats closes to the user.
Here is the code:

-(void)locationManager:(CLLocationManager*)_manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation
{
    self.userLocation = newLocation;
    NSLog(@"Usser location is: %@", self.userLocation);
    NSLog(@"USer location latitude = %.4f", self.userLocation.coordinate.latitude );
    NSLog(@"USer location longitude = %.4f", self.userLocation.coordinate.longitude);
    if (self.userLocation)
    {
        [self.locMgr stopUpdatingLocation];
    }
    [self calculateShortestDistanceBetweenUserAndBiz:self.userLocation];
}

//==============================================================================
-(void)calculateShortestDistanceBetweenUserAndBiz:(CLLocation *)_userLocation
{
    NSLog(@"Entering %s", __FUNCTION__);
    for(LocationMapAnnotation *tempAnnot in self.mapAnnotations)
    {
        CLLocation *tempLocation = [[CLLocation alloc] initWithLatitude:tempAnnot.coordinate.latitude longitude:tempAnnot.coordinate.longitude];
        NSLog(@"tempLocation created %@ ", tempLocation);
        CLLocationDistance tempDistance = [_userLocation distanceFromLocation: tempLocation];
        NSLog(@"tempDistance  was calculated at %d", tempDistance);
        if(!self.shortestDistance) 
        {
            self.shortestDistance = tempDistance;
            NSLog(@"Assigned value %d to shortestDistance", self.shortestDistance);
            continue;
        }
        if(self.shortestDistance >= tempDistance)
        {
        NSLog(@"Replacing shortest distance value from %d to %d", self.shortestDistance, tempDistance);
            self.shortestDistance = tempDistance;
        }
    }
}

The method works fine and here is the console output:

Created a new locationmodel

Usser location is: <+43.47878384, -80.53074371> +/- 1629.00m (speed -1.00 mps / course -1.00) @ 11-05-22 8:58:10 PM Eastern Daylight Time

USer location latitude = 43.4788

USer location longitude = -80.5307

Entering -[locationModel calculateShortestDistanceBetweenUserAndBiz:]

tempLocation created <+43.69445000, -79.75080000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 629486151

Assigned value 629486151 to shortestDistance

tempLocation created <+43.62192000, -79.52238000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 209234637
tempLocation created <+43.59233000, -79.54258000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at -1442027836
tempLocation created <+43.57980000, -79.61713000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at -680604989

tempLocation created <+43.55260000, -79.58553000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at -1882725832
tempLocation created <+43.58191000, -79.71417000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 947780311

Replacing shortest distance value from 629486151 to 1089496493

tempLocation created <+43.65530000, -79.41298000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 1561068529

tempLocation created <+43.64854000, -79.38776000> +/- 0.00m (speed -1.00 mps / course -1.00) 
tempDistance  was calculated at 466814513

tempLocation created <+43.47690000, -80.52500000> +/- 0.00m (speed -1.00 mps / course -1.00) 
tempDistance  was calculated at -1816387020
Replacing shortest distance value from 947780311 to 1089489801
Received memory warning. Level=1

My question is now that the function is working, Im trying to understand what the values mean? For instance the values assigned to shortest distance and tempdistance are huge numbers but what do they represent? How do I know the function is working fine. I dont understand. I want to be able to use this value of shortest distance to set my my MKMapview here, I wont to replace the line below with the value which corresponds to the shortest distance from the cetercoord which is the user’s location

MKCoordinateRegion preRegion = MKCoordinateRegionMakeWithDistance(centerCoord, 15000, 15000); 

Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:28:32+00:00Added an answer on May 22, 2026 at 6:28 pm

    CLLocationDistance is of type double so you need to use %f instead of %d (which is for ints). This is probably why you are seeing very large meaningless numbers.

    Assuming shortestDistance is also declared as a CLLocationDistance, do the same when logging that as well.

    CLLocationDistance is in meters which is also what MKCoordinateRegionMakeWithDistance expects for the last two parameters so that line could be:

    MKCoordinateRegion preRegion = MKCoordinateRegionMakeWithDistance
        (centerCoord, shortestDistance, shortestDistance); 
    

    Another unrelated thing:

    You have a memory leak: you need to release tempLocation after the call to distanceFromLocation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array of objects with created images (Object.Image), i want to show images
I have array of keywords. How can I find if any of these keywords
I have array like this: array( 'person0' => array( 'name'=>'name0','address'=>'address0' ), 'person1' => array(
I have a array (dataArray) of NSDictionary item. It has datas like david for
I created an object called Participant. Now I want to have an array of
I have array object in jquery. array=[]; array{Id:user.Id,Date:user.Date}; Now am going to pass this
I have array like below the page. I want to find array number which
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
if i have array array[0] = jack; array[1] = jill; array[2] = lisa; array[2]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.