Map view not showing exact location needs to zoom out - ios

I am new to ios and working on map view i have showned position on map view (Annotation)using lat, long. map view is showing location, but need to do pinching to get exact location.(zoom with two fingures) I am using following code but it not showing exact location for exact location i need to use zoom then is showing exact location but i want to display it without touching to map view??
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];
mapView.centerCoordinate = annotationPoint.coordinate;

You need to work out the region you want the map to be looking at and then use [mapView setRegion:newRegion animated:YES] ref.
An MKCoordinateRegion is made of a CLLocationCoordinate2D which you've already made and a MKCoordinateSpan. Here's an example of making a span
MKCoordinateSpan span;
span.latitudeDelta = 1.5;
span.longitudeDelta = 1.0;
MKCoordinateRegion newRegion;
newRegion.center = zoomLocation;
newRegion.span = span;
[mapView setRegion:newRegion animated:YES];

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

How to set-up zoom level in iOS?

I am using the code given below to set a region in map view:
CLLocationCoordinate2D loc = (CLLocationCoordinate2D )[self geoCodeUsingAddress:searchBar.text];
CLLocationDistance visibleDistance = 100000;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, visibleDistance, visibleDistance);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
[self.mapView setRegion:adjustedRegion animated:YES];
[self.mapView setCenterCoordinate:loc];
[self.mapView setZoomEnabled:YES];
It shows the entire map in a small area. How can increment the zoom level?
You can also take a help of span value for MapView. Animate to that small area and span map to zoom for required value.
Use below code:
CLLocationCoordinate2D loc = (CLLocationCoordinate2D )[self geoCodeUsingAddress:searchBar.text];
[UIView animateWithDuration:1.5 animations:^{
MKCoordinateRegion region;
region.center.latitude = loc.latitude;
region.center.longitude = loc.longitude;
region.span.latitudeDelta = 0.15f; // Decrement value to zoom
region.span.longitudeDelta = 0.15f; // Decrement value to zoom
[self.mapView setRegion:region animated:YES];
} completion:^(BOOL finished) { }];
Change the visibleDistance to a smaller number. Experiment until you find something you like.
Finally I have solved the issue by removing
[self.mapView setCenterCoordinate:loc];
I worked well when i removed the above.
Try this,
CLLocationCoordinate2D loc = (CLLocationCoordinate2D )[self geoCodeUsingAddress:searchBar.text];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, visibleDistance, visibleDistance);
region.span.latitudeDelta = 0.15f;
region.span.longitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
[self.mapView setZoomEnabled:YES];

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

How to zoom out on load for mkmapview in iOS 7

This code I have works for iOS 6 but doesn't seam to have an effect on iOS 7. How can I get it to work for iOS 7 when setting the span doesn't have an effect?
CLLocationCoordinate2D ctrpoint = self.location.coordinate;
MapPoint *mp = [[MapPoint alloc] initWithCoordinate:ctrpoint title:[NSString stringWithFormat:#"%#: %#",NSLocalizedString(#"Pin Name:", #"Pin Name:"),self.pin.name] subtitle:self.pin.cell];
[self.mapView addAnnotation:mp];
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
MKCoordinateRegion region;
region.span = span;
region.center = self.location.coordinate;
[self.mapView setRegion:region animated:YES];
the proper way is to use the constant provided:
- (MKCoordinateRegion)regionForWorld {
return MKCoordinateRegionForMapRect(MKMapRectWorld);
}
so
- (void)zoomToWorldAnimated:(BOOL)animated {
MKCoordinateRegion region = [self regionForWorld];
[self.mapView setRegion:region animated:animated];
}
Assuming that self.mapView is already synthesised I would do something like this:
MKCoordinateRegion region = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance([self.location.coordinate, 800, 800)];
region.span.latitudeDelta = 0.2;
region.span.longitudeDelta = 0.2;
[self.mapView setRegion:region animated:YES];
Please note that the 800 values in MKCoordinateRegionMakeWithDistance are "CLLocationDistance latitudinalMeters" and "CLLocationDistance longitudinalMeters". You might have to change these depending on your map size and the zoom level you want.
For the map point I would do something like this:
MKPointAnnotation *mapPoint = [[MKPointAnnotation alloc] init];
[mapPoint setCoordinate:self.location.coordinate];
[mapPoint setTitle:#"Your title"];
[mapPoint setSubtitle:#"My subtitle"];
[mapView addAnnotation:mapPoint];
I have tested this in both iOS6 and iOS7 and can assure you it works just fine.

How to give a location coordinate to plot a point in a map in ios using storyboard?

I am creating a map view in a view controller, using storyboard.
When I use the following code.
-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationDistance distance = 1000;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,
distance,
distance);
MKCoordinateRegion adjusted_region = [self.mapView regionThatFits:region];
[self.mapView setRegion:adjusted_region animated:YES];
}
A point is plotted in San Francisco, CA, United States. The
userLocation coordinates are the predefined value in MapKit.h framework.
Now I create a
-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationDistance distance = 1000;
CLLocationCoordinate2D myCoordinate;
myCoordinate.latitude = 13.04016;
myCoordinate.longitude = 80.243044;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(myCoordinate,
distance,
distance);
MKCoordinateRegion adjusted_region = [self.mapView regionThatFits:region];
[self.mapView setRegion:adjusted_region animated:YES];
}
Here, the region is displayed having the coordinate in the center. But, no point is plotted at the coordinate position.
How to plot a point or annotation in that coordinate location?
Try this code inside of didUpdateUserLocation method
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D myCoordinate;
myCoordinate.latitude=13.04016;
myCoordinate.longitude=80.243044;
annotation.coordinate = myCoordinate;
[self.mapView addAnnotation:annotation];
Add this code in didUpdateUserLocation
MKAnnotation *annotation = [[MKAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
[myMap addAnnotation:annotation];
Try This..
//MAP VIEW Point
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude=latitude;
center.longitude=longitude;
//Span
MKCoordinateSpan span;
span.latitudeDelta=THE_SPAN;
span.longitudeDelta=THE_SPAN;
myRegion.center=center;
myRegion.span=span;
//Set our mapView
[MapViewC setRegion:myRegion animated:YES];
//Annotation
//1.create coordinate for use with the annotation
CLLocationCoordinate2D wimbLocation;
wimbLocation.latitude=latitude;
wimbLocation.longitude=longitude;
Annotation * myAnnotation= [Annotation alloc];
myAnnotation.coordinate=wimbLocation;

Resources