user1104325
user1104325

Reputation: 161

Highlight annotation in map view

For an iPad application I have SplitView with a TableView on the left side showing a list of Restaurants. The right side has a MapView showing annotations of those restaurants.

I'm trying to zoom to an Annotation and somehow highlight it when the user clicks on the related restaurant in the table on the left side.

Unfortunately I don't get the map to reload.

Any suggestions or ideas how to solve this ?

Here's my ViewDidLoad

- (void)viewDidLoad {

    NSString *docsDir;
    NSArray *dirPaths;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];

    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"smshipaddec2011.db"]];
    const char *dbpath = [databasePath UTF8String];

    MapViewAnnotation *newAnnotation;

    sqlite3_stmt *statement;

    if (sqlite3_open(dbpath, &smshdb2) == SQLITE_OK) {

        NSString *querySQL = [NSString stringWithFormat: @"SELECT vmapx, vmapy, vname, vsection FROM SMSHVENUES"];

        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(smshdb2, query_stmt, -1, &statement, NULL) == SQLITE_OK) {

            while(sqlite3_step(statement) == SQLITE_ROW) {

                double latitude = sqlite3_column_double(statement, 0);
                double longitude = sqlite3_column_double(statement, 1);

                CLLocationCoordinate2D location;
                location.latitude = latitude;
                location.longitude = longitude;

                newAnnotation = [[MapViewAnnotation alloc] initWithTitle:[[NSString alloc] initWithUTF8String:
                                                                          (const char *) sqlite3_column_text(statement, 2)] andCoordinate:location];
                [mapView addAnnotation:newAnnotation];
                [newAnnotation release];

            } 

            sqlite3_finalize(statement);

        } 

        sqlite3_close(smshdb2);
    }



    mapView.showsUserLocation = YES;

    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];

    CLLocation *userLoc = mapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 

    //this is Shanghai:
    region.center.latitude = 31.227849 ;
    region.center.longitude = 121.473770;

    region.span.longitudeDelta = 0.02f;
    region.span.latitudeDelta = 0.02f;

    [mapView setRegion:region animated:YES];

    [super viewDidLoad];

}

Now when someone clicks on a Table cell "configureView" is triggered which works fine. But then I don't know how to move the map, zoom to that Annotation and maybe change the colour or make it bigger.

- (void)configureView {

    // animate map
    // zoom to annotation
    // make it bigger or change colour

}

Really appreciate any help.

Upvotes: 0

Views: 754

Answers (1)

doclaphroaig
doclaphroaig

Reputation: 51

MKMapView has simple API for this:

In response to your user's selection, you call something like:

[mapView setRegion: animated:];

The region should be suitably zoomed in on the thing you are showing.

Upvotes: 1

Related Questions