Want to get a different mapview when a UItableViewCell is selected - ios

As part of my app, one of my tableviews has seven rows/cells. When you click on each of the rows, it will display it's data to a detailed view, changing UILabels with the appropriate data pertaining to this row/cell which was selected. I'm using a mysql database and convert it to json to get the date for the tableview row/cells as well as the detailed view.
In the mysql database, each of the columns which contain a name, street address, longitude and latitude coordinates pertaining to the street addresses.
My tableview as well as my detailed views are populating the correct data. However I'd also like to have subview in my detailed view to contain a mapview, which changes per the street address or the coordinates when the the detailed view shows that particular data.
I can create a mapview/subview in my detailedview.m file as per the code below, but the map stays the same for each row/cell I select. I would like map to have different coordinates per row/cells selected and the data display in the detailed view.
//Create NSCC MapView
MKMapView * mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 446, 768, 415)];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 40.494828;
region.center.longitude = -74.443016;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
//Set PIN Location for NSCC on MKMapView
NSCCDisplayMap *ann = [[NSCCDisplayMap alloc] init];
ann.title = #"NSCC New Brunswick";
ann.subtitle = #"18 Paterson Street New Brunswick, NJ 08901";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
//Add NSCC MKMapView object as a subview to our view
[self.view addSubview:mapView];
So I guess my real question is: ???
When I select the row/cell in my tableview and it displays the json data from the database correctly in my detailedView for each of the seven rows/cells, how can I get the mapview to change to the appropriate coordinates pertaining to that data displayed in the detailedView?

If you look at these lines of your code -
region.center.latitude = 40.494828;
region.center.longitude = -74.443016;
then it clearly indicates that you are not using different latitude and longitude to change the region of MKMapView, instead of this you hardcoded some values here.
What you need to do here is when you select a row in UITableView then you have to pass coordinates corresponding to that row to your detail view. Then use those coordinates to change the region of MKMapView.

Related

iOS mapview - zoom to pin and then activate callout

I have a mapview in xcode, which is all working well.
What my page does just now is like this:
downloads a bunch of data and locations from a backend database
populates a mapview with locations and drops pins
populates a table underneath the mapview
That all works great, and I end up with a mapview with a load of pins, and a tableview that has the details of those pins.
What I want to do now, is allow the user to tap on a row from the tableview, and have the map zoom and centre to the corresponding map pin, and then automatically activate the annotation pin callout.
In my 'didselectrow' method, I have the following:
MKCoordinateSpan span = MKCoordinateSpanMake(0.1f, 0.1f);
CLLocationCoordinate2D coordinate = { [item.latitude floatValue], [item.longitude floatValue] };
MKCoordinateRegion region = { coordinate, span };
[self.mapView setRegion:region animated:YES];
This works great too. Tapping on the table row will zoom to and centre the map pin at this location.
I just can't get the last step of firing the annotation pin callout to work.
I have tried:
[mapview annotationsInMapRect:mapview.visibleMapRect];
But this isn't working, and it is possible that there still might be 2 or 3 map pins in the visible area.
What I need to do is to get the pin nearest to the centred location (see above - item.latitude / item.longitude) to automatically open it's callout.
Everything in the code is set up and working, and the map pins have callouts that fire when tapped on, I just need this last stage of having the pin nearest the centre location to open automatically.
Can anyone help with this?
I have tried various other suggestions on SO, but none seem to fit this requirement.
I think I have got solution for your problem you need to use this [_mapView setSelectedAnnotations:#[[[self.mapView annotations] lastObject]]];
For testing I have created an small project that have these 2 methods.
- (IBAction)buttonTouched:(id)sender {
[_mapView showAnnotations:[self.mapView annotations] animated:YES];
[self performSelector:#selector(showAnnotationCallOut) withObject:nil afterDelay:1.0f];
}
- (void) showAnnotationCallOut {
[_mapView setSelectedAnnotations:#[[[self.mapView annotations] lastObject]]];
}
Note: I have called just one annotation for test that why I am calling last object. You'll need to call it for specific annotation of your annotation array.
Edit: According to Richerd's comment here is solution for problem of finding the annotion and showing the callout fro that.
for (MapViewAnnotation *annotion in [self.mapView annotion]) {
if ([annotion.identifire isEqualToString:annotationToCallCallOutIdentifier]) {
//[_mapView setSelectedAnnotations:#[annotation]];
[_mapView selectAnnotation:annotation animated:YES];
break;//don't break if there are can be more than one callouts
}
}

MKCoordinateRegionMakeWithDistance displaying wrong coordinates

I'm trying to set approx a 10 mile range around the current location of the user when my mapView loads.
Here is my Objective C code in viewDidLoad:
self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
[self.view addSubview:self.mapView];
//Set initial scope of map
CLLocationDistance mapWidith = 16000;
CLLocationDistance mapHeight = 16000;
CLLocationCoordinate2D userLocation = self.mapView.userLocation.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation, mapHeight , mapWidith);
[self.mapView setRegion: region
animated:false];
When I run it's starting with the correct zoom (I think) but randomly centered in the atlantic ocean somewhere. Not sure if I'm accessing the userLocation coordinate correctly but it seems right. Im pretty new to MKMapView so Im struggling a bit.
The userLocation property on mapView isn't populated yet. You should implement the mapView:didUpdateUserLocation: method and after that point you can reliably use that property (or use the location sent to you).

Map view not focus on annotation

Hello i am new to ios and showing an annoatation on map view using lat and long coming from server.
I have one view controller in which i am adding map and showing position of lat and long. But my problem is whenever i am adding annotation map is not focusing on annotattion insted of it everytime i want pinch map view then map is going to at annoatation i dont know why this is happening?
zoomLocation.latitude = latmpa.doubleValue;
zoomLocation.longitude = logmpa.doubleValue;
annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = #"masjid....";
[mapView selectAnnotation:annotationPoint animated:YES];
[mapView addAnnotation:annotationPoint];
here is my code adding in viewwillappera
You need to center the map by use the following code.
mapView.centerCoordinate = annotationPoint.coordinate;
Milan's solution does what you need without animation.
If you want to do this with animation, you should use:
[mapView setCenterCoordinate:location animated:YES];

Change the position of Map point without changing zoom level or span

I am using following codes
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04), Some KM, Some KM);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
Otherway around is
MKCoordinateRegionMake(CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04), ADD SPAN HERE)
Both of these makes the map zoom. How is it possible that I change the Region without any zoom.
Get the current region and just change the center point.
// get current region
MKCoordinateRegion region = self.mapView.region;
// Update the center
region.center = CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04);
// apply the new region
self.mapView.region = region;
If you have a coordinate to set, use the MKMapView -setCenterCoordinate:animated: method. Animating the position change gives the user a clue about what's happened.
CLLocationCoordinate2D coordinate = annView.annotation.coordinate;
[myMapView setCenterCoordinate:coordinate animated:YES];
Also, no need to make a new coordinate, just use the one already in the annotation view.

iOS map-kit view only certain region in map

I'm new to iOS map-kit, even though i have loaded mapview in my app using the following code
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516;
zoomLocation.longitude= -76.580806;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
// 3
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
// 4
[_mapView setRegion:adjustedRegion animated:YES];
I want to allow user only to view a certain region not the whole world in the map, For example i take america, i want to allow user to view only america in the map, where he can't able to view any other places. What have to do to achieve this.. Needs your help.
You need to set the zoom level to a particular country or a particular region..... i think you must have a look at this.... that helped me when i was trapped in such kind of confusion... i hope that might help you too....

Resources