Map view not focus on annotation - ios

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];

Related

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).

creating draggable pin annotation but should stop it from dragging on a button

I have created draggable pin annotation and it is working perfectly fine, the problem is i don't want to drag and drop pins on a particular button click and even don't want to reload map as i am having overlays on it, i have used DDAnnotation class for draggable pins
DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate:theCoordinate addressDictionary:nil];
annotation.title = #"Testing";
annotation.subtitle = #"Drag and Drop Pin";
[self.detailMapView addAnnotation:annotation];
Thanks
Any help would be really appreciable!
Please set [annotation setDraggable:NO];

Want to get a different mapview when a UItableViewCell is selected

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.

Map view span changes when updating the location

I have a function in my view controller with a mapkit that is called when the location changes. I would like set to the map view so that is is centered around the current location and moves with it as it updates.
It works in a sense i.e it tracks along, however it is always zoomed in really far. If I zoom out it snaps back to where it was when it calls the update again after getting a new location.
in from params CLLocation *loc
bool first = TRUE;
if(first){
first=FALSE; // bit of a bodge...if i set this to false it will track the update possition but not at the desired "zoom" its look far to close.
CLLocation *loc = [[CLLocation alloc] initWithLatitude:54.976619 longitude:-1.613118];//newcastle city center.
CLLocationDegrees spanLat = 1000;
CLLocationDegrees spanLng = 1000;
userLocation = MKCoordinateRegionMakeWithDistance(loc.coordinate, spanLat, spanLng);
[mapview setRegion:userLocation animated:TRUE];
}else {
CLLocationDegrees spanLat = [mapview region].span.latitudeDelta;// keep the originals? DOESN'T WORK!!
CLLocationDegrees spanLng = [mapview region].span.longitudeDelta;//
userLocation = MKCoordinateRegionMakeWithDistance(loc.coordinate, spanLat, spanLng);
[mapview setRegion:userLocation animated:TRUE];
}
Just keep setting the center coordinate and don't set the region.
By the way, you do know that you don't need any of that code, right? You can just tell the mapview to track the device's current location. See my book for an example:
http://www.apeth.com/iOSBook/ch34.html#_map_kit_and_current_location

iOS SDK Mapview Annotation callout redraw error

I'm trying to add the distance from the user's position to a selected annotation's subtitle in a mapview. The mechanics of it are working, but the actual callout gets messed up the first time it's displayed. There appears to be a redraw problem.
Subsequent taps on the pin show the correct layout.
Here's the relevant code:
// called when selecting annotations
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
MKPointAnnotation *selectedAnnotation = view.annotation;
//attempt to add distance on annotation
CLLocation *pointALocation = [[CLLocation alloc]
initWithLatitude:selectedAnnotation.coordinate.latitude
longitude:selectedAnnotation.coordinate.longitude];
float distanceMeters = [pointALocation distanceFromLocation:locationManager.location];
//for sending info to detail
myPinTitle = selectedAnnotation.title;
[selectedAnnotation setSubtitle:[NSString stringWithFormat:#"%.2f miles away", (distanceMeters / 1609.344)]];
}
I've tried calling [view setNeedsDisplay], but to no avail.
Thanks in advance for your help.
The Solution that Worked
Here's the solution I finally came up with. It seems to work.
I edited out the duplicate code from the didSelectAnnotationView method, above, and came up with:
//called when user location changes
- (void)updatePinsDistance
{
for (int x=0; x< [[mapView annotations]count]; x++) {
MKPointAnnotation *thisPin =[[mapView annotations] objectAtIndex:x];
//attempt to add distance on annotation
CLLocation *pointALocation = [[CLLocation alloc]
initWithLatitude:thisPin.coordinate.latitude
longitude:thisPin.coordinate.longitude];
float distanceMeters = [pointALocation distanceFromLocation:locationManager.location];
NSString *distanceMiles = [NSString stringWithFormat:#"%.2f miles from you",
(distanceMeters / 1609.344)];
[thisPin setSubtitle:distanceMiles];
}
}
You should set your subtitle in another place than didSelectAnnotationView. Actually all annotationViews should have their title and subtitle set before they are returned by the mapView:viewForAnnotation: method.
The fact that you set a long subtitle certainly explains that the callout is not the right size. The size must be calculated before the didSelectAnnotationView is called.

Resources