Deepak Terse
Deepak Terse

Reputation: 712

IOS - Optimizing google maps markers

I am working on google maps in iOS which has a lot of markers in it (spprox. 10 to 20). I tried optimising my code.

For creating markers we call the createMarker() method.

Previous code :

func createMarker(){
    //Creating UIImageView() object for each marker
    let backgroundImg = UIImageView()
    backgroundImg.image = UIImage(named: "JobMarkerInvitedBg")
}

Optimized code :

let uiImageView = UIImageView()
func createMarker(){
    //Reusing UIImageView() object
    let backgroundImg = uiImageView
    backgroundImg.image = UIImage(named: "JobMarkerInvitedBg")
}

Here, I just created the imageview object once and reused for all the markers instead of creating it again and again. Will this make any significant difference in memory or cpu consumption?

Upvotes: 3

Views: 1253

Answers (2)

christopher.online
christopher.online

Reputation: 2774

Reuse the same instances of UIImage and maybe store them as global properties somewhere as shown below. You should not reuse the same UIImageView instance. Preloading images and using the same instance of a UIImage will give you the most performance benefits. An enum was used for the ImageStore because that enum cannot be instantiated.

enum ImageStore {
    static let jobMarker = UIImage(named: "JobMarkerInvitedBg")
}

class MarkCreator {

    func createMarkerView() -> UIImageView {
        let imageView = UIImageView()
        imageView.image = ImageStore.jobMarker
        return imageView
    }
}

Upvotes: 3

slushy
slushy

Reputation: 12385

If you are creating several markers with the same image, use the same instance of UIImage for each of the markers. This helps improve the performance of your application when displaying many markers.

https://developers.google.com/maps/documentation/ios-sdk/marker

You want to use the same instance of UIImage. And yes, calling the same instance is how Google wants you to place multiple markers.

Upvotes: 3

Related Questions