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

  • Home
  • SEARCH
  • 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 8599825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:28:46+00:00 2026-06-12T01:28:46+00:00

I have some locations ( in this case >3000 ) stored with Core Data.

  • 0

I have some locations ( in this case >3000 ) stored with Core Data. Once I open the map, I fetch the locations and store them in an array. Each time the mapview region is changed I call a function which will calculate which annotations are visible in the current visibleMaprect and filter them by pixel-distance. ( I know there would be more complex optimizations, like quadtrees, but I would not really implement it right now, if it’s not extremely necessary ).
This is my code :

//locations is an array of NSManagedObjects     
for (int i =0 ; i < [locations count]; i++)
        {
            // managed object class for faster access, valueforkey takes ages ...
            LocationEntity * thisLocation = [locations objectAtIndex:i];
            CLLocationCoordinate2D coord  = CLLocationCoordinate2DMake( [thisLocation.latitude doubleValue],  [thisLocation.longitude doubleValue]) ;
            // mapRect is mapView.visibleMapRect
            BOOL isOnScreen = MKMapRectContainsPoint(mapRect, MKMapPointForCoordinate(coord));      
            if (isOnScreen)
            {
                CGPoint cgp =  [mapView convertCoordinate:coord toPointToView:mapView];
                // compare the distance to already existing annotations
                for (int idx = 0; idx < [annotations count] && hasEnoughDistance; idx++)    
                {        
                    CGPoint cgp_prev = [mapView convertCoordinate:[[annotations objectAtIndex:idx] coordinate] toPointToView:mapView];
                    if ( getDist(cgp, cgp_prev) < dist )    hasEnoughDistance = FALSE;
                }
            }
            if (hasEnoughDistance)
                // if it's ok, create  the  annotation, add to an array and after the for add all to the map
        }

The map is freezing for a few seconds after each zoom/movement.
I checked with time profiler and the simple obtainment of coordinates is taking sometimes 1 whole second, sometimes just 0.1, even though the coordinates are indexed attributes in my model… Also these type of lines seem to take ages :
CGPoint cgp = [mapView convertCoordinate:coord toPointToView:mapView];

Any suggestions how could I calculate the pixel/point distance between two annotations/coordinates without going through this function ? Or any optimization suggestions for Core Data?

Thanks 🙂

  • 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-06-12T01:28:48+00:00Added an answer on June 12, 2026 at 1:28 am

    Ok, I sort of missed the not having them too close bit from your explanation. The conversion between the coordinates is very slow. The way you can alleviate it is to precompute the coordinates into map points with MKMapPointForCoordinate and store them persistently – they only depend on the coordinates. Then you can quickly calculate the distance between the map points of two annotations, scale it depending on your current zoom level of the map and this will quite closely relate to the actual distance on the screen. It should be accurate enough and will be much faster.

    I would recommend calculating the squared distance and comparing it to squared dist. You would be saving a lot on the sqrt().

    If you still get boggled down on getDist() (or getSqDist()) you could either go for a kd tree or use the Accelerate Framework to do the calculations. I’ve done the latter when I needed to calculate distances between many points and the speedup was very good. But the details of this is an another cup of tea. Let me know if you need any help with that.

    The fact that your coordinates are indexed would only help if you actually searched for annotations by the coordinates, so it won’t help if you just look through all of them.

    A way of dealing with long loading times from CoreData would be to try making your annotations as lightweight as possible, so only storing the coordinates and map points. Then you could have a way of getting the rest of the annotation data as needed. This could be done with the proxy pattern.

    One more thing. Fast enumeration might be faster and is better practice as well, so

    for(LocationEntity* thisLocation in locations)
    

    instead of

    for (int i =0 ; i < [locations count]; i++)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some static text that needs to show up at 2 locations within
I want to set some locations and have my app check if its by
I have some data loaded as a np.ndarray and need to convert it to
I have some arbitrary pixel data that I want to save as a PNG.
I have some library of classes, working with my data, which is being read
I'm currently working on an app using MVVM that needs to have some data
i have a table array dynamically generated from a data query and stored in
I have a function that pulls data from two locations and places the returned
At some point I must have moved files from one location to another using
I'm setting some Microdata for products sold by 3rd parties. I have the location

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.