Updating MKMapView while UITableView is scrolling, obtaining coordinates from cell

Hello,

I have an UIViewController containing a MKMapView and an UITableView. Each cell in the tableView contains a set of coordinates. If you want to see, please check this screenshot

I want to the update MKMapView “in real time” as the tableview is scrolling with contains coordinates.

I don’t need to use the didSelectRow method. Because user does not select yet.

What is the right code blocks for scrollViewWillBeginDragging method?

  func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    print("scrollViewWillBeginDragging")
 }

Thank you

@durul Thanks very much for your question!

I’m a bit confused with your problem. You have one map, but a UITableView that shows coordinates in each row. Is the map displaying all visible rows in the mapView, or just the top row? You might want to clarify this point.

Now having said that, when it comes to smooth scrolling of your UITableView displaying data which will appear on the map, the question to ask is, where is the data coming from which is populating the UITableView? Is ti coming from the cloud via a webservice call, or is it coming locally from the device itself via a persistent store like Core Data? If the data is coming from an API cloud via a webservice, then I would suggest you use UITableView’s prefetch method’s to handle this. Here is a link to the documentation from Apple.

The purpose of the prefetch methods will help you obtain the data prior to the user’s scrolling of the UITableView. Now, if you’re interested in obtaining the data from those cells which are VISIBLE to the user, then I would recommend using the indexPathsForVisibleRows method. Here is another link to documentation from Apple on how to use it. From there, you will receive an array of those cells which are visible, and you should be able to obtain the coordinates that you need to display in your mapView. :slight_smile:

I hope this helps!

All the best!

1 Like

Hello syedfa,

Thanks for answering me. Yes Map displaying all latitude and longitude information. You can see with this screenshot Imgur: The magic of the Internet. Actually I had shared with my first comment.

The data is coming from an Yelp API.

I will check these links.

Thank you syedfa

Great! One other recommendation I would make is to make sure you incorporate some form of paging when your user scrolls. In other words, make sure you are not prefetching ALL records, but a short amount each time the user scrolls to view the data. This way, you minimize the impact on the user’s data plan, but also minimize the storage, and processing impact on your app, and the user’s device.

All the best!

1 Like

Hey syedfa,

THANK YOU! Task is completed.

FANTASTIC! Very happy to hear this :slight_smile:

All the best!

May I ask, what was your solution?

of course

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    for visibleIndexPath in tableView.indexPathsForVisibleRows! {

        let business = destinations![(visibleIndexPath as NSIndexPath).row]
        let region = MKCoordinateRegionMakeWithDistance(business.coordinate, 250, 250)
        mapView.setRegion(region, animated: true)
    }
}

Congratulations! I’m happy to have helped!

All the best!

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.