I have this code that I need altered slightly. The code I have below creates a pin on my current location (perfectly as I might add).
The only problem is that I need the code only to be run when I click a button, not every time I move.
Here is the code of the current location and the pin.
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView setDelegate:self];
[self.mapView setShowsUserLocation:YES];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
// zoom to region containing the user location
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 700, 700);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
// add the annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = #"Your Car";
//point.subtitle = #"";
[self.mapView addAnnotation:point];
}
I need this to run when I click a button such as:
-(IBAction)addPin
{
}
In the viewDidLoad: method,
Create a button like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(showPinOnClick)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(100.0, 200.0, 150.0, 60.0);
[self.view addSubview:button];
// create the method defined as selector method for the UIButton in your viewcontroller
-(void) showPinOnClick{
//paste your code here to show the pin
CLLocationCoordinate2D location = self.mapview.userLocation.coordinate;
MKCoordinateRegion region;
MKCoordinateSpan span;
location.latitude = -32.008081;
location.longitude = 115.757671;
span.latitudeDelta = 0.03;
span.longitudeDelta = 0.03;
region.span = span;
region.center = location;
[_mapview setRegion:region animated:YES];
[_mapview regionThatFits:region];
//Create your annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
// Set your annotation to point at your coordinate
point.coordinate = location;
point.title = #"Your Car";
//Drop pin on map
[_mapview addAnnotation:point];
[_mapview selectAnnotation:point animated:NO];
}
Related
Whenever MKMapView is initialised and set to a location
- (void) setMapLocationItem:(Location*)geofence{
if(self.mapView==nil){
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.mapView setDelegate:self];
[self.view addSubview:self.mapView];
[self.view sendSubviewToBack:self.mapView];
[self.view sendSubviewToBack:self.collectionView];
}
[self.mapView setHidden:NO];
[_mapView removeAnnotations:_mapView.annotations];
CLLocationCoordinate2D coordinate;
coordinate= CLLocationCoordinate2DMake(geofence.latitude.doubleValue, geofence.longitude.doubleValue);
CGFloat radius = 110;
if([geofence.radius boolValue])
radius = [geofence.radius floatValue];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (coordinate, radius, radius);
[_mapView setRegion:region animated:NO];
self.annotation = [[MKPointAnnotation alloc] init];
self.annotation.coordinate = coordinate;
self.annotation.title = geofence.name;
self.annotation.subtitle = geofence.address;
[_mapView addAnnotation:self.annotation];
}
The memory used my the map is never released whenever the viewController is closed.
Its is only getting released when another MKMapView is initialized. Any thoughts as too why this is?
I have referred the solutions in this question iOS6 MKMapView Memory Issue
I started programming recently, and I have a question! I have created a mapview with the annotation and disclosure button.
How can I create multiple annotations with disclosure button that, after a tap, go to different descriptions based on the annotation choice?
This is my code:
#synthesize mapView;
-(void)viewDidLoad {
[super viewDidLoad];
[mapView setMapType:MKMapTypeHybrid];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion bigBen = { {0.0, 0.0} , {0.0, 0.0} };
bigBen.center.latitude = 51.50063;
bigBen.center.longitude = -0.124629;
bigBen.span.longitudeDelta = 0.02f;
bigBen.span.latitudeDelta = 0.02f;
[mapView setRegion:bigBen animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"Big Ben";
ann1.subtitle = #"Your subtitle";
ann1.coordinate = bigBen.center;
[mapView addAnnotation: ann1];
MKCoordinateRegion Bridge = { {0.0, 0.0} , {0.0, 0.0} };
Bridge.center.latitude = 51.500809;
Bridge.center.longitude = -0.120914;
Bridge.span.longitudeDelta = 0.02f;
Bridge.span.latitudeDelta = 0.02f;
[mapView setRegion:Bridge animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = #"Westminster Bridge";
ann2.subtitle = #"Your subtitle";
ann2.coordinate = Bridge.center;
[mapView addAnnotation:ann2];
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
MyPin.pinColor = MKPinAnnotationColorPurple;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop=TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
The usual way to do it is to implement -mapView:annotationView:calloutAccessoryControlTapped: in your map view delegate. You can use the annotaionView parameter to decide which annotaion was tapped and what information you want to display.
I'm struggling with setting the subtitle of an annotation in MapView, once it has been clicked.
.m:
MKCoordinateRegion England = { {0.0, 0.0} , {0.0, 0.0} };
England.center.latitude = 51.50063;
England.center.longitude = -0.124629;
England.span.longitudeDelta = 100.0f;
England.span.latitudeDelta = 100.0f;
[mapView setRegion:England animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"England";
ann1.subtitle = #"subtitle";
ann1.coordinate = England.center;
[mapView addAnnotation:ann1];
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation {
MKPinAnnotationView *MyPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"]; MyPin.pinColor = MKPinAnnotationColorPurple;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop = TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
- (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)view
{
id<MKAnnotation> selectedAnn = [mapView.selectedAnnotations objectAtIndex:0];
country_title = selectedAnn.title;
selectedAnn.subtitle = #"NEW SUBTITLE";
}
How do I set the subtitle once the annotation has been clicked? I wan't to do it that way, because I get some rows from mysql when it's clicked.. but I don't know how to update the subtitle for the specific annotation?
Thanks in advance! :)
id<MKAnnotation> selectedAnn = view.annotation;
[mapview removeAnnotation:selectedAnn];
.. Add Annotation with new subtitle
[mapview addAnnotation:newselectedAnn];
[newselectedAnn setSelected:Yes animated:No];
Hope this helps
I have a button when if pressed I want it to get the users location and drop an annotation on it. But it seems that I press it once, it adds the annotation in the middle of the ocean then my users location appears (which works). I press it the second time and it adds the annotation on my location.
Code:
mapView.showsUserLocation = YES;
MKCoordinateRegion newRegion;
newRegion.center.latitude = mapView.userLocation.location.coordinate.latitude;
newRegion.center.longitude = mapView.userLocation.location.coordinate.longitude;
newRegion.span.latitudeDelta = 0.0004f;
newRegion.span.longitudeDelta = 0.0004f;
[mapView setRegion: newRegion animated: YES];
CLLocationCoordinate2D coordinate;
coordinate.latitude = mapView.userLocation.location.coordinate.latitude;
coordinate.longitude = mapView.userLocation.location.coordinate.longitude;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate: coordinate];
[annotation setTitle: #"Your Car"];
[annotation setSubtitle: #"Come here for pepsi"];
[mapView addAnnotation: annotation];
[mapView setZoomEnabled: YES];
[mapView setScrollEnabled: YES];
It sounds like that you are asking for the userlocation before the CLLocationManager have found it.
By setting mapView.showsUserLocation = YES; the mapview starts locating the user with a locationManager asynchronously so it probably won't have a correct location immediately.
You need to set up a custom CLLocationManager and start its location update on your button press then set the the annotation in its delegate when it have found a location.
I created a simple application with the MapKit framework. When it starts, the user location is zoomed in and shown. But when I zoom out and scroll around the map, it automatically centralizes it self to the user location again after a couple of seconds. How can I stop this?
In the m-file:
#import "APPNAMEViewController.h"
#import <MapKit/MapKit.h>
#synthesize mapview;
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)aUserLocation {
mapView.scrollEnabled = YES;
mapView.zoomEnabled = YES;
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = aUserLocation.coordinate.latitude;
location.longitude = aUserLocation.coordinate.longitude;
region.span = span;
region.center = location;
[mapView setRegion:region animated:NO];
[mapView setUserTrackingMode:MKUserTrackingModeNone animated:NO];
}
- (void)viewDidLoad
{
[super viewDidLoad];
mapview = [[MKMapView alloc]
initWithFrame:CGRectMake(0,
44,
self.mapview.bounds.size.width,
self.mapview.bounds.size.height)
];
mapview.showsUserLocation = YES;
mapview.mapType = MKMapTypeStandard;
mapview.delegate = self;
[mapview setUserTrackingMode:MKUserTrackingModeNone animated:NO];
[self.view addSubview:mapview];
}
Please help me :)
Best regards
Michal
Remove [mapView setRegion:region animated:NO]; in - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)aUserLocation
[mapView setRegion:region animated:NO]; //Changes the currently visible region and optionally animates the change.
Remove : [mapView setUserTrackingMode:MKUserTrackingModeNone animated:NO];
This makes the map center on current location.