Hi I have this code to zoom in on the users location.
-(IBAction)getLocation{
mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0}, {0.0, 0.0} };
region.center = mapview.userLocation.coordinate;
region.span.longitudeDelta = 0.1f;
region.span.latitudeDelta = 0.1f;
[mapview setRegion:region animated:YES];
}
On the first press of the button the map moves to coordinate 0,0, however on any subsequent presses it works as it should and takes the map to the users location. I cannot figure out why it does not work on the first go but works everytime after that??
Try this:
- (IBAction)showCurrentLocation:(id)sender {
self.mapview.showsUserLocation=YES;
self.mapview.delegate = self;
[self.mapview setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
Related
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];
}
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];
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.
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];
I have placed two pins on a map, each with an annotation when clicked, however, only the annotation for StoreLocationOne is showing. Both pins on the map are being displayed but StoreLocationTwo annotation is not showing when clicked, any ideas?
-(void)viewDidLoad {
[super viewDidLoad];
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = 57.132053;
region.center.longitude = -2.135592;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
StoreLocationOne *ann = [[StoreLocationOne alloc] init];
ann.title = #"Heavenly Pizzas Mannofield";
ann.subtitle = #"483a Great Western Rd, Aberdeen, AB10 6NN";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
MKCoordinateRegion region2 = { {0.0, 0.0 }, {0.0, 0.0 } };
region2.center.latitude = 57.232458;
region2.center.longitude = -2.347853;
region2.span.longitudeDelta = 0.01f;
region2.span.latitudeDelta = 0.01f;
StoreLocationTwo *ann2 = [[StoreLocationTwo alloc] init];
ann2.title2 = #"Heavenly Pizzas Kintore";
ann2.subtitle2 = #"School Road, Kintore, AB51 0UU";
ann2.coordinate = region2.center;
[mapview addAnnotation:ann2];
}
The title and subtitle properties must be named exactly that. The map view won't know to look for title2 and subtitle2.
You can have multiple classes that implement MKAnnotation but the property names must be as per the protocol.
Additionally, if all you need are the properties coordinate, title, and subtitle, you could use the built-in annotation class MKPointAnnotation instead of creating a separate class for each coordinate.