How to Set a Region for a MKMapView? - ios

I am new in iOS apps development and I want to put a Map in My application with a pushpin on an exact location using its latitude and longitude. I add the Map but the problem is it always appear with its initial position an the push pin doesn't appear (It is on the map but you need to change the position to see it ) . Like this figures shows :
Initial Position
2.- After I moved the map's position
What I want is to show the position of the pushpin as a default location and with a zoom .
This is a sample of My Code :
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationCoordinate2D myCoordinate = {20.5, -7.06}; //AS an Example
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = myCoordinate;
//Drop pin on map
[self.mapView addAnnotation:point];
//Region with Zoom
-(void)viewWillAppear:(BOOL)animated {
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 20.5;
zoomLocation.longitude= -7.6;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
[self.mapView setRegion:viewRegion animated:YES];
}
I used this code but still the same thing it always display the initial position (Figure 1)
Thank You .

(void)viewDidLoad
{
[super viewDidLoad];
MapAnnotation *ann = [[MapAnnotation alloc] init];
MKCoordinateRegion region;
region.center.latitude = 31.504679;
region.center.longitude = 74.247429;
region.span.latitudeDelta = 0.01;
region.span.longitudeDelta = 0.01;
[mapView setRegion:region animated:YES];
ann.title = #"Digital Net";
ann.subtitle = #"Office";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}
(MKAnnotationView *) mapView:(MKMapView *)mpView viewForAnnotation:(id)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation) {
static NSString *defaultID = #"myLocation";
pinView = (MKPinAnnotationView *)[mpView dequeueReusableAnnotationViewWithIdentifier:defaultID];
if(pinView == nil) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultID];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
}
return pinView;
}
I have done something like that and it was working fine

Related

Map View - Current Location after moving the map view

Loaded Map view on the screen.
If we move the map from left and right
i need to get the current location on the screen immediatly when i click a button
i used the following code to get the current location initially
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[self.mapView addAnnotation:point];
}
by clicking the button call below method.
- (void)gotoLocation
{
float spanX = 0.05;
float spanY = 0.05;
MKCoordinateRegion region;
region.center.latitude = mapView.userLocation.coordinate.latitude;
region.center.longitude = mapView.userLocation.coordinate.longitude;
region.span.latitudeDelta = spanX;
region.span.longitudeDelta = spanY;
[mapView setRegion:region animated:YES];
}

not able to add annotation to mapView iOS from a different method

I'm trying to add a annotation to the map from a different button method, however it doesn't show up. It doesn't give an error either.
Where as all the annotations which I have mentioned in the mapView method, they are being displayed.
-(void) testButton {
/// custom location coordinate
CLLocationCoordinate2D userLocationNew2 = CLLocationCoordinate2DMake(-80.050003, -13.416667);
// use that cusom coordinate
MKCoordinateRegion region2 = MKCoordinateRegionMakeWithDistance(userLocationNew2, 20000, 20000);
[mapView setRegion:[self.mapView regionThatFits:region2] animated:YES];
// Add an annotation
MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init];
point2.coordinate = userLocationNew2;
point2.title = #"Check";
// point2.subtitle = #"Right here oooooo!!!";
[mapView addAnnotation:point2];
}
The other method
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
/// custom location coordinate
CLLocationCoordinate2D userLocationNew2 = CLLocationCoordinate2DMake(-73.050003, -13.416667);
// use that cusom coordinate
MKCoordinateRegion region2 = MKCoordinateRegionMakeWithDistance(userLocationNew2, 20000, 20000);
[self.mapView setRegion:[self.mapView regionThatFits:region2] animated:YES];
// Add an annotation
MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init];
point2.coordinate = userLocationNew2;
point2.title = #"Basic";
}

MKPinAnnotationView color is not working

I am trying to show some pins from an array, it shows them all but they are red, and not green as i ask them to be.
Why is that ?
//run on array to get all locations
for(int k=0;k<[array count];k=k+2)
{
float targetlat=[[array objectAtIndex:k] floatValue];
float targetlongi=[[array objectAtIndex:k+1] floatValue];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(targetlat,targetlongi);
NSString *partyTitle = #"title";
MKPinAnnotationView *partyPin = [self returnPointView:location andTitle:partyTitle andColor:MKPinAnnotationColorGreen];
[self.mapView addAnnotation:partyPin.annotation];
}
//function callback is working but its red, and it takes so much time to load
-(MKPinAnnotationView*) returnPointView: (CLLocationCoordinate2D) location andTitle: (NSString*) title andColor: (int) color
{
MKCoordinateRegion region = self.mapView.region;
region.center = location;
region.span.longitudeDelta /= 5.0;
region.span.latitudeDelta /= 5.0;
[self.mapView setRegion:region];
MKPointAnnotation *resultPin = [[MKPointAnnotation alloc] init];
MKPinAnnotationView *result = [[MKPinAnnotationView alloc] initWithAnnotation:resultPin reuseIdentifier:Nil];
[resultPin setCoordinate:location];
resultPin.title = title;
result.pinColor = color;
return result;
}
Regarding the main issue that the pins are red instead of green:
The code creates an MKPinAnnotationView but this view is never given to the map view.
To make the map view use annotation views that you create, you must implement the viewForAnnotation delegate method and return them from there.
Otherwise, the map view has no knowledge of annotation views that you create.
If you don't implement viewForAnnotation, the map view creates a default red pin view.
Regarding the second issue that "it takes so much time to load":
The most likely reason for this is that you are calling setRegion each time you add an annotation.
If you are adding, say, 500 annotations, the map view is setting the region 500 times.
Please note that it is not necessary to call setRegion simply to add an annotation (regardless of the currently-visible region). The annotation's coordinate does not have to be "visible" to add an annotation there.
What you want to do inside the for loop is simply construct a region that includes all the annotations and then call setRegion (or setVisibleRect) once and after all the annotations are added (after the for loop). Constructing an MKMapRect and calling setVisibleMapRect is easier than constructing an MKCoordinateRegion in order to call setRegion.
In iOS 7, this is even simpler: Just call showAnnotations (no manual construction necessary).
Example:
//Initialize the MKMapRect (region) we want to show to null...
MKMapRect showMapRect = MKMapRectNull;
for(int k=0;k<[array count];k=k+2)
{
float targetlat=[[array objectAtIndex:k] floatValue];
float targetlongi=[[array objectAtIndex:k+1] floatValue];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(targetlat,targetlongi);
NSString *partyTitle = #"title";
//Here, don't create the annotation view.
//Just create the annotation...
MKPointAnnotation *resultPin = [[MKPointAnnotation alloc] init];
[resultPin setCoordinate:location];
resultPin.title = partyTitle;
[self.mapView addAnnotation:resultPin];
//Add this annotation's coordinate
//to the MKMapRect we want to show...
MKMapPoint annMapPoint = MKMapPointForCoordinate(location);
MKMapRect annMapRect = MKMapRectMake(annMapPoint.x, annMapPoint.y, 0, 0);
showMapRect = MKMapRectUnion(showMapRect, annMapRect);
}
mapView.visibleMapRect = showMapRect;
//In iOS 7, instead of constructing MKMapRect manually,
//we could just call showAnnotations...
//[mapView showAnnotations:mapView.annotations animated:YES];
//Implement the viewForAnnotation delegate method...
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
//if annotation is the user location,
//return nil so map view shows default view for it (blue dot)...
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString *reuseId = #"pin";
MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (pav == nil)
{
pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
pav.canShowCallout = YES;
pav.animatesDrop = YES;
pav.pinColor = MKPinAnnotationColorGreen;
}
else
{
pav.annotation = annotation;
}
return pav;
}

adding a drop pin button

I'm creating a button that will place a pin on users current location. But when ever I press it, instead is is placed in the middle of the global map, around Africa, I believe this is because in the original tutorial where I got this from, they created a void method with "didUpdateUserLocation" but I can figure a way to add this to the IBAction , here's what am working with. Has anyone ever had trouble with this?
This is the zooming in code but notice the didUpdateUserLocation down there vvvv
-(void)mapView: (MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
MKCoordinate region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,
800,800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];
}
`
Now here is the IBAction that when tapped, drops pin around Africa or the center of the global map. I figured it's because it is missing the didUpdateUserLocation
-(IBAction)addAnnotation;
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,
800,800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];
MKPointAnnotation *point = [[MKPointAnnotation Alloc] init];
point.coordinate = myLocation.coordinate;
point.title = #"Test";
point.subtitle = #"Text";
[self.mapView addAnnotation:point];
}
So if anybody would know how to, what i figure is the solution, didUpdateUserLocation to the IBAction, please let me know, appreciate it!
Try this...
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation
{
MKCoordinateRegion region;
CLLocationCoordinate2D location;
location.latitude = aUserLocation.coordinate.latitude;
location.longitude = aUserLocation.coordinate.longitude;
region.span = aMapView.region.span;
region.center = location;
[self.objMapView setRegion:region animated:FALSE];
showCurrentLoc = FALSE;
}
-(IBAction)addAnnotation;
{
MKCoordinateRegion region;
region.span = self.mapView.region.span;
region.center = location;
[self.mapView setRegion:region animated:FALSE];
MKPointAnnotation *point = [[MKPointAnnotation Alloc] init];
point.coordinate = self.mapView.userLocation.coordinate;
point.title = #"Test";
point.subtitle = #"Text";
[self.mapView addAnnotation:point];
}

Incorrect user location shown on MKMapView after scaling the map programmatically

I have MKMapView which have UserTrackingMode = MKUserTrackingModeFollow,
and I have adding a circle overlay to show a region of the certain diameter at user location.
Also user can change the diameter of the region so I want to scale the map to insure whole region/circle is shown on that portion of the map.
The problem I have now is that scaling the map number of times by setting region results in incorrect user location annotation - it is moved from the correct location.
I cannot understand why is that happens, I see in the debugger that the mapView.userLocation property have correct coordinates.
But once new update is happens or I move the map manually - the annotation jumps to the correct place.
This is the code:
- (void)addCircleWithRadius:(double)radius andCoordinate:(CLLocationCoordinate2D)coordinate
{
_regionCircle = [MKCircle circleWithCenterCoordinate:coordinate radius:radius];
[_regionCircle setTitle:#"Region"];
MKCoordinateRegion region;
region.center.latitude = coordinate.latitude;
region.center.longitude = coordinate.longitude;
region.span.latitudeDelta = 0.00002 * _regionCircle.radius;
region.span.longitudeDelta = 0.00002 * _regionCircle.radius;
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits: region];
[self.mapView setRegion:adjustedRegion animated:TRUE];
_circleView = nil;
[self.mapView addOverlay:_regionCircle];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if(!_circleView)
{
_circleView = [[MKCircleView alloc] initWithCircle:overlay];
_circleView.strokeColor = [UIColor blueColor];;
_circleView.fillColor = [UIColor blueColor];
_circleView.alpha = 0.25;
_circleView.lineWidth = 2.0;
}
return _circleView;
}
- (IBAction)regionSliderValueChanged:(id)sender
{
[self updateRadiusCircle];
}
- (void) updateRadiusCircle
{
[self.mapView removeOverlays:self.mapView.overlays];
CLLocationCoordinate2D myCoordinate = {_currentLocation.coordinate.latitude, _currentLocation.coordinate.longitude};
[self addCircleWithRadius:self.radiusSlider.value andCoordinate:myCoordinate];
}
I have published the video on YouTube to better understand the issue.
https://www.youtube.com/watch?v=474gdjkGwJA

Resources