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.
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 have a map view in my app, and every time I add a third annotation (pin) it shows the ARGV running error in the mainDelegate.m file? Code below:
[super viewDidLoad];
MKCoordinateRegion region = { {0.0,0.0}, {0.0,0.0}};
region.center.latitude = 53.321927;
region.center.longitude = -6.250504;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.001f;
[mapview setRegion:region animated:YES];
MapPin *sandford = [[MapPin alloc] init];
sandford.title = #"Sandford Park";
sandford.subtitle = #"Ranelagh";
sandford.coordinate = region.center;
[mapview addAnnotation:sandford];
MKCoordinateRegion region2 = { {0.0,0.0}, {0.0,0.0}};
region2.center.latitude = 53.324089;
region2.center.longitude = -6.252080;
region2.span.longitudeDelta = 0.01f;
region2.span.latitudeDelta = 0.001f;
[mapview setRegion:region2 animated:YES];
MapPin *Cinnamon = [[MapPin alloc] init];
Cinnamon.title = #"Cinnamon";
Cinnamon.subtitle = #"Ranelagh";
Cinnamon.coordinate = region2.center;
[mapview addAnnotation:Cinnamon];
MKCoordinateRegion region3 = { {0.0,0.0}, {0.0,0.0}};
region3.center.latitude = 53.325010;
region3.center.longitude = -6254461;
region3.span.longitudeDelta = 0.01f;
region3.span.latitudeDelta = 0.001f;
[mapview setRegion:region3 animated:YES];
MapPin *Gommels = [[MapPin alloc] init];
Gommels.title = #"Gommels";
Gommels.subtitle = #"Ranelagh";
Gommels.coordinate = region3.center;
[mapview addAnnotation:Gommels];
The first two work fine but the third stops the simulator every time
-6254461 seems pretty big for a longitude value. I presume it should be -6.254461.
You blew the map's mind, it couldn't cope with that value.
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];
}
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
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];
}