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 9108103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:43:58+00:00 2026-06-17T02:43:58+00:00

I need to add on my MKMapView several annotations (example one for each city

  • 0

I need to add on my MKMapView several annotations (example one for each city in a country) and I don’t want to initializate every CLLocation2D with latitude and longitude of all cities. I think it is possible with arrays, so this is my code:

NSArray *latit = [[NSArray alloc] initWithObjects:@"20", nil]; // <--- I cannot add more than ONE object
NSArray *longit = [[NSArray alloc] initWithObjects:@"20", nil]; // <--- I cannot add more than ONE object

// I want to avoid code like location1.latitude, location2.latitude,... locationN.latitude...

CLLocationCoordinate2D location;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

for (int i=0; i<[latit count]; i++) {
    double lat = [[latit objectAtIndex:i] doubleValue];
    double lon = [[longit objectAtIndex:i] doubleValue];
    location.latitude = lat;
    location.longitude = lon;
    annotation.coordinate = location;
    [map addAnnotation: annotation];
}

Ok, its all right if I leave ONE object in the NSArrays latit and longit, I have ONE annotation on the map; but if I add more than one object to the arrays app builds but crashes with EXC_BAD_ACCESS (code=1…). What’s the problem, or what the best way to add several annotations without redundant code? Thank you!

  • 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-17T02:43:59+00:00Added an answer on June 17, 2026 at 2:43 am

    You are using always the same annotation object, which is this:

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    

    Displace it in the for loop, or copy it before adding to the map.

    Explanation: This is the annotation property found in the MKAnnotation protocol:

    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    

    As you see it isn’t copying the object, so if you add always the same annotation you would have duplicate annotations. If you add an annotation with coordinates (20,20) at time 1, then at time 2 you change the annotation coordinates to (40,40) and add it to the map, but it’s the same object.

    Also I don’t suggest to put NSNumber objects inside it. Instead make an unique array and fill it of CLLocation objects, as they’re made to store coordinates. The CLLocation class has this property:

    @property(readonly, NS_NONATOMIC_IPHONEONLY) CLLocationCoordinate2D;
    

    It’s an immutable object too, so you need to initialise this property at the moment you create the object. Use the initWithLatitude:longitude: method :

    - (id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude;
    

    So you could write a better version of your code:

    #define MakeLocation(lat,lon) [[CLLocation alloc]initWithLatitude: lat longitude: lon];
    
    NSArray* locations= @[ MakeLocation(20,20) , MakeLocation(40,40) , MakeLocation(60,60) ];
    
    for (int i=0; i<[locations count]; i++) {
        MKPointAnnotation* annotation= [MKPointAnnotation new];
        annotation.coordinate= [locations[i] coordinate];
        [map addAnnotation: annotation];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't access a element with its href. Like this example, i need add
I have a MKMapView. I need to add a circle as a radius around
I need add paging to panel, which populated with dynamically-created controls. I want implement
I need to display a text on Mkmapview for example 100 meters, i know
I need add Auto complete on apex Tabular Form. there is a column as
I need add a new user group for mediawiki. The new group has more
Hї! I have wrote test for my application. I need add item to database
Need to add two same name .csproj class libraries in my solution.Have two project
need to add a 2nd css stylesheet to a page. do i add a
I need to add recurring events with start time and end time. I used

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.