I am showing a map view on front page of my application, I set the region and coords from current location using CLLocation.
When the application is run, a blue screen is being displayed on the mapview, when I open its child views and come back to main screen, the map show perfectly with location and zoom level.
Is there any thing wrong in the following code which causes the blue screen?
- (void)viewWillAppear:(BOOL)animated {
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
CLLocation *location = [lm location];
CLLocationCoordinate2D coord;
coord = [location coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
My guess would be that you are seeing blue because the location is (0, 0) which is in the middle of the sea. Probably because the first time round, the location on the CLLocationManager is not set yet.
You should probably check if there's a location yet and if not then wait for a callback in the CLLocationManagerDelegate method locationManager:didUpdateToLocation:fromLocation: and then centre the map on the location you want.
Something like this:
...
#property (nonatomic, assign) BOOL needsCentering
...
#synthesize needsCentering = _needsCentering;
- (void)viewWillAppear:(BOOL)animated {
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
CLLocation *location = [lm location];
if (!location) {
_needsCentering = YES;
} else {
_needsCentering = NO;
CLLocationCoordinate2D coord;
coord = [location coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (_needsCentering) {
_needsCentering = NO;
CLLocationCoordinate2D coord;
coord = [newLocation coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
}
But also, you might want to take a look at showsUserLocation of MKMapView.
Related
I need to centerlized current location user pic on MKView. But curretnly it is not in center as given in below screen shot.
-
(void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error
{
NSLog(#"didFailWithError: %#", error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil)
{
NSLog(#"%#",[NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude]);
NSLog(#"%#",[NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude]);
// [mkMapView setCenterCoordinate:mkMapView.userLocation.location.coordinate animated:YES];
}
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 150, 150);
[mkMapView setRegion:[mkMapView regionThatFits:region] animated:YES];
}
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = aLocation .coordinate.latitude;
location.longitude = aLocation .coordinate.longitude;
region.span = span;
region.center = location;
[aMapView setRegion:region animated:YES];
}
MKCoordinateSpan span;
MKCoordinateRegion region;
span.longitudeDelta =0.001;
span.latitudeDelta =0.001;
region.span = span;
region.center.latitude = self.mapVIew.region.center.latitude;
region.center.longitude = self.mapVIew.region.center.longitude;
[self.mapVIew setRegion:region];
Will Help
Use this code,
[yourMapViewName setCenterCoordinate:yourMapViewName.userLocation.location.coordinate animated:YES];
or
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 100, 100);
[yourMapViewName setRegion:[yourMapViewName regionThatFits:region] animated:YES];
}
hope its helpful
Try this, hope will be helpfull.
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [yourLatitude doubleValue];//locationManager.location.coordinate.latitude;
region.center.longitude = [yourLongitude doubleValue];// locationManager.location.coordinate.longitude;
region.span.latitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[yourMapView setRegion:region animated:YES];
Use the Below delegate method, its working for me,
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//Get the east and west points on the map so you can calculate the distance (zoom level) of the current map view.
MKMapRect mRect = YourMapViewName.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
//Set your current distance instance variable.
//Set your current center point on the map instance variable.
coordinate = YourMapViewName.centerCoordinate;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
self.locationManager1.distanceFilter = kCLDistanceFilterNone;
self.locationManager1.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager1 startUpdatingLocation];
//View Area
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude =coordinate.latitude;
region.center.longitude=coordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[YourMapViewName setRegion:region animated:YES];
}
hope its helpful
I got solution . Now my fb pic coming on centre of the map. When i put (-30,-30) coordinate on my profileImageView frame.
profileImageView.frame = CGRectMake(-30, -30, 54, 54);
profileImageView.layer.masksToBounds = YES;
profileImageView.layer.cornerRadius = 27;
How can I set the longitude and latitude using a textfield? I have tried making my textfield a double but it just seems to crash.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = *(myTextField);
region.center.longitude = *(myTextField1);
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Test";
ann.subtitle = #"test";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
How I can make this work?
MKPointAnnotation *annonation = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D mycordinate;
mycordinate.latitude = [self.latitude floatValue];
mycordinate.longitude =[self.longitude floatValue];
annonation.coordinate = mycordinate;
[self.mapview addAnnotation:annonation];
In self.latitude store your latitude value and
in self.longitude store your longitude value.
Try below Code:
Just get text from textFields and set latitude,longitude float value from text.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = [self.myTextField.text floatValue];
region.center.longitude = [self.myTextField1.text floatValue];
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Test";
ann.subtitle = #"test";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
I am new for iOS, and working on google maps.
I would like to save my travelled route map (with polyline) into my database like Runtastic Application.
Currently getting polyline when i am moving.
this is the code till i am using.
- (void)startLocationUpdates
{
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeFitness;
self.locationManager.distanceFilter = 8; // meters
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
for (CLLocation *newLocation in locations) {
if (self.locations.count > 0) {
self.distance += [newLocation distanceFromLocation:self.locations.lastObject];
CLLocationCoordinate2D coords[2];
coords[0] = ((CLLocation *)self.locations.lastObject).coordinate;
coords[1] = newLocation.coordinate;
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 300, 300);
[self.mapView setRegion:region animated:YES];
[self.mapView addOverlay:[MKPolyline polylineWithCoordinates:coords count:2]];
}
[self.locations addObject:newLocation];
}
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolyline *polyLine = (MKPolyline *)overlay;
MKPolylineRenderer *aRenderer = [[MKPolylineRenderer alloc] initWithPolyline:polyLine];
aRenderer.strokeColor = [UIColor blueColor];
aRenderer.lineWidth = 3;
return aRenderer;
}
return nil;
}
Now i want save that map details into database and retrieve it again and show it as a MapView.
Thank you in advance.
In this code I receive a wrong distance, _latitude and _longtitude are NSString.
Any suggestion to correct this?
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coord;
coord.latitude = [_latitude doubleValue];
coord.longitude = [_longitude doubleValue];
CLLocation *anotLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
_distancia = [NSNumber numberWithFloat:([location distanceFromLocation:anotLocation]/1000)];
check out this singleton implementation of location controller
https://github.com/hackerinheels/locationcontroller
with this just call
CLLocation *currLocation = [[LocationController sharedLocationInterface]getCurrentLocation];
i want the map to get the location of the user and he can change the location by taping on the map. The problem is when tap on location the method (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation , called and the map show both the current location and the location he tapped in!! i want only one location >>>
here is the code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[mapView addGestureRecognizer:singleTap];
}
- (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation:(MKUserLocation *)userLocation
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
longitudeLabel.text = [NSString stringWithFormat:#"%.8f", userLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:#"%.8f", userLocation.coordinate.latitude];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[mapView setRegion:[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!!!";
[mapView addAnnotation:point];
}
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
CLLocationCoordinate2D coord = [mapView convertPoint:[sender locationInView:mapView] toCoordinateFromView:mapView];
NSLog(#"Map touched %f, %f.", coord.latitude, coord.longitude);
longitudeLabel.text = [NSString stringWithFormat:#"%.8f", coord.longitude];
latitudeLabel.text = [NSString stringWithFormat:#"%.8f", coord.latitude];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 800, 800);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = coord;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[mapView removeAnnotations:[mapView annotations]];
[mapView addAnnotation:point];
}
Firstly, on ask one question on each page. If I answer question 2 right and someone else answers question 1 right, which one gets marked as the right answer?
Secondly, the method didUpdateUserLocation is called by iOS when it gets new information about where the device is. You don't need to add a new annotation there or you'll end up with lots of annotations for every time the device moves. If you want to show the user's current location call mapView.showsUserLocation = YES;. Once the user has tapped and you've created a new annotation, you can turn the userLocation off by setting it to NO.