iOS - MKMapView - Buggy Annotation - ios

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.

Related

Purging MKMapView's memory on iOS8

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

Only add pin when button is clicked

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

MKMap View showing Only Grid not Showing Map View

In my app i am showing some place on map view using MKMapView . It was showing map fine. But from yesterday it is showing only grid and not showing the map , when i add delegate method it goes in the mapviewdidfiailtoload sometime.
I am using the below code
//mapview
myMapView=[[MKMapView alloc] initWithFrame:CGRectMake(0,gameNameLabel.frame.size.height, 320, 181)];
myMapView.mapType=MKMapTypeStandard;
myMapView.delegate=self;
LocationModel *loc=presentGame.location;
NSArray *cordinates=[loc.geoCordinates componentsSeparatedByString:#","];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = [[cordinates objectAtIndex:0] doubleValue];
zoomLocation.longitude= [[cordinates objectAtIndex:1] doubleValue];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
// 3
MKCoordinateRegion adjustedRegion = [myMapView regionThatFits:viewRegion];
// 4
[myMapView setRegion:adjustedRegion animated:YES]; [scroll addSubview:myMapView];
Place *pin = [[Place alloc] init];
pin.name = loc.gameLocation;
pin.latitude = [[cordinates objectAtIndex:0] doubleValue];
pin.longitude = [[cordinates objectAtIndex:1] doubleValue];
PlaceMark* Place1 = [[PlaceMark alloc] initWithPlace:pin];
Place1.tagg=1;
[myMapView addAnnotation:Place1];
//end of mapview
- (MKAnnotationView *)mapView:(MKMapView *)mapView1 viewForAnnotation:(id )annotation {
static NSString *identifier = #"PlaceMark";
if ([annotation isKindOfClass:[PlaceMark class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView1 dequeueReusableAnnotationViewWithIdentifier:identifier];
annotationView.tag=[mapView1.annotations count];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
return annotationView;
}
return nil;
}
METERS_PER_MILE is defined as 1609.344
and i have set delegate in .h file .
All thing was working fine but now it showing the grid only , it showing Pin atleaset
I had the same problem MKMap View showing Only Grid not Showing Map View. I found out it was related to a change I made to the time/date on my mac. When I fixed the time and date to current time, the map worked again.
The iOS simulator comes with the official maps app included. Open that and verify that it can get tiles, it could be a network issue. If the Maps app isn't showing any tiles then you have network issues.
Can you try by turning off the animation by changing the line
[myMapView setRegion:adjustedRegion animated:YES];
By
[myMapView setRegion:adjustedRegion animated:NO];

iOS - MKMapView - Draggable Annotations

I have the annotation ready to go, but trying to figure out on how to make it draggable with my code:
-(IBAction) updateLocation:(id)sender{
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 is parked here"];
[annotation setSubtitle: #"Come here for pepsi"];
[mapView addAnnotation: annotation];
[mapView setZoomEnabled: YES];
[mapView setScrollEnabled: YES];
}
Thanks in advance!
To make an annotation draggable, set the annotation view's draggable property to YES.
This is normally done in the viewForAnnotation delegate method.
For example:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
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.draggable = YES;
pav.canShowCallout = YES;
}
else
{
pav.annotation = annotation;
}
return pav;
}
If you need to handle when the user stops dragging and drops the annotation, see:
how to manage drag and drop for MKAnnotationView on IOS?
In addition, your annotation object (the one that implements MKAnnotation) should have a settable coordinate property. You are using the MKPointAnnotation class which does implement setCoordinate so that part's already taken care of.

Objective-c - Stop centralizing the user location when the map updates its self

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.

Resources