Well it is really a silly question & confusing.
I am trying to get the map to span & focus on certain region , so I set the location as Userlocation, it is perfect.
Although when I save the user location & try to upload them in viewDidLoad, it doesn't work.
- (IBAction)GetcurrentLocation1:(id)sender{
Maplocation.showsUserLocation=YES;
MKCoordinateRegion mapRegion;
mapRegion.center = Maplocation.userLocation.coordinate;
NSLog(#"The USer Location are :%f %f",Maplocation.userLocation.coordinate.latitude,Maplocation.userLocation.coordinate.longitude);
mapRegion.span.latitudeDelta = 0.2;
mapRegion.span.longitudeDelta = 0.2;
self.currentProduct.shop.longcoordinate=Maplocation.userLocation.coordinate.longitude;
self.currentProduct.shop.latcoordinate=Maplocation.userLocation.coordinate.latitude;
[Maplocation setRegion:mapRegion animated: YES];
}
-(void)showtheuseronmap:(double)longtitude:(double)latitude{
if(!annotation){
annotation =[[MyAnotation alloc]initWithCoordinate:CLLocationCoordinate2DMake(latitude, longtitude) title:[self.currentProduct.shop shopname] subtitle:#"The shop is here"];
}
[Maplocation addAnnotation:annotation];
MKCoordinateRegion mapRegion;
CLLocationCoordinate2D locationsh=CLLocationCoordinate2DMake(latitude, longtitude);
mapRegion.center=locationsh;
NSLog(#"The USer Location are :%f",mapRegion.center.latitude);
mapRegion.span.latitudeDelta= 0.2;
mapRegion.span.longitudeDelta= 0.2;
[Maplocation setRegion:mapRegion animated:YES];
//Maplocation.showsUserLocation=YES;
}
in viewDidLoad
[self showtheuseronmap:self.currentProduct.shop.longcoordinate :self.currentProduct.shop.latcoordinate];
in NSLog , I can see the coordinates correctly.
I am really confused, I don't know how this can happen?
Related
I have my app on two devices, one on an iOS7 enabled device and another on an iOS8 enabled device. My MKMapView is not center on the users location on the iOS8 device but is on the iOS7.
Here is my code, I have requested the use of the users location on another screen.
-(void)viewDidLoad{
[LocationManager sharedInstance];
CLLocation * location = [[LocationManager sharedInstance] getCurrentLocation];
NSLog(#"Location %f %f",location.coordinate.latitude,location.coordinate.longitude);
MKCoordinateRegion mapRegion;
mapRegion.center = location.coordinate;
mapRegion.span.latitudeDelta = 0.01;
mapRegion.span.longitudeDelta = 0.01;
NSLog(#"Map Region Lat %f",mapRegion.center.latitude);
NSLog(#"Map Region Long %f ",mapRegion.center.longitude);
self.getDirectionsMap.delegate = self;
[self.getDirectionsMap setRegion:mapRegion animated: NO];
self.getDirectionsMap.showsUserLocation=YES;
self.getDirectionsMap.showsPointsOfInterest=YES;
}
My Location Manager object .m
+ (LocationManager *)sharedInstance {
if (nil == kLocationManager) {
kLocationManager = [[LocationManager alloc] init];
}
return kLocationManager;
}
-(CLLocation *)getCurrentLocation{
CLLocation *loc = [_locationManager location];
return loc;
}
- (id)init {
self = [super init];
if (!self) return nil;
/* setup location manager */
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
//iOS8 check
if ([_locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
}
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[_locationManager setDistanceFilter:DISTANCE_FILTER];
return self;
}
My map successful shows the users position but map is not centered. My location also gets logged correctly with the correct coordinates, and this also matches the logged Map region lat and long.
You viewDidLoad method has not checked that there is actually a location to use yet. If you used a CLLocationManager you could set it going and then it would call the delegate's methods when it has an actual location. I suspect that the iOS device is taking longer to get a GPS reading and it is giving you nil in viewDidLoad.
Can anybody help me please? I'm new to xcode and am baffled by what I think is a small problem. For some reason the first time my map is opened from my initial view controller the pin is dropped but the map does not zoom to the correct region even though the latitude and longitude are correct.
When I go back to the first view controller and launch it again the zoom works.
My code is in the view controllers implementation file under viewdidload.
-(void)viewDidLoad {
[super viewDidLoad];
NSLog(#"lat in map %#", eventLat);
NSLog(#"long in map %#", eventLong);
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = [eventLat doubleValue];
region.center.longitude = [eventLong doubleValue];
region.span.latitudeDelta = 0.01f;
region.span.longitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MMAnnotate *ann = [[MMAnnotate alloc] init];
ann.title = #"Test Title";
ann.subtitle = #"Test Subtitle";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
You should try doing that in viewDidAppear instead. If you're prepared for it to zoom to that location every time the view appears. viewDidLoad is too early in the mapview cycle.
This is a little strange, but I have been trying to find a solution for 2 days straight, to set the map region but nothing seems to work
Here is my code :
-(void)viewWillAppear:(BOOL)animated{
//[self step1locationupdate];
Maplocation.showsUserLocation=YES;
MKCoordinateRegion mapRegion;
mapRegion.center.longitude=self.currentProduct.shop.longcoordinate;
mapRegion.center.latitude=self.currentProduct.shop.latcoordinate;
NSLog(#"The USer Location are :%f",mapRegion.center.latitude);
mapRegion.span.latitudeDelta= 2;
mapRegion.span.longitudeDelta= 2;
[Maplocation setRegion:mapRegion animated:YES];
NSLog(#"The USer Location are :%f %f",Maplocation.userLocation.coordinate.latitude,Maplocation.userLocation.coordinate.longitude);
}
The NSLog for malocation.userlocation.coordinate are always 0 when it starts.
The same code I have added it into the viewDidLoad part & also there was no difference
Kindly help
I think you need to initialize your MKCoordinateRegion before you start using it. Something like 'MKCoordinateRegion mapRegion = MKMakeRegionWithCoordinates(......'
Well I have saved the above method in a (void) method , then I have fired it after 5 Sec from viewWillAppear method.
-(void)viewWillAppear:(BOOL)animated{
[self performSelector:#selector(updatecurrentLocation) withObject:nil afterDelay:5];
}
-(void)updatecurrentLocation{
Maplocation.showsUserLocation=YES;
MKCoordinateRegion mapregion;
mapregion.center=Maplocation.userLocation.coordinate;
mapregion.span.latitudeDelta = 2;
mapregion.span.longitudeDelta = 2;
[Maplocation setRegion:mapregion animated: YES];
}
That is it folks :)
I have used the classes provided by apple CrumbPath.o and CrumbPathView.o, but it supports only iphone 5.0,when I try the same code with iphone 4.0 ,it does not update the route.
Code :
if (newLocation)
{
if (oldLocation.coordinate.latitude == 0.0) {
initialLocation = [[[CLLocation alloc]initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude]retain];
}
// make sure the old and new coordinates are different
if((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
(oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
{
if (!crumbs)
{
// This is the first time we're getting a location update, so create
// the CrumbPath and add it to the map.
//
crumbs = [[CrumbPath alloc] initWithCenterCoordinate:newLocation.coordinate];
[mapView addOverlay:crumbs];
// On the first location update only, zoom map to user location
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
[mapView setRegion:region animated:YES];
}
else
{
// This is a subsequent location update.
// If the crumbs MKOverlay model object determines that the current location has moved
// far enough from the previous location, use the returned updateRect to redraw just
// the changed area.
//
// note: iPhone 3G will locate you using the triangulation of the cell towers.
// so you may experience spikes in location data (in small time intervals)
// due to 3G tower triangulation.
//
Count++;
double latitude = 0.000500 * Count;
double longitude = 0.000020 * Count;
_bean = [[TempBean alloc]init];
_bean.lat = newLocation.coordinate.latitude + latitude;
_bean.lon = newLocation.coordinate.longitude - longitude;
CLLocation *locationToDraw = [[CLLocation alloc]initWithLatitude:_bean.lat longitude:_bean.lon];
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Update_Loc" message:[NSString stringWithFormat:#"Lat:%f , Lon:%f",locationToDraw.coordinate.latitude,locationToDraw.coordinate.longitude] delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#"Cancel",nil];
// [alert show];
// [alert release];
MKMapRect updateRect = [crumbs addCoordinate:locationToDraw.coordinate];
if (!MKMapRectIsNull(updateRect))
{
// There is a non null update rect.
// Compute the currently visible map zoom scale
MKZoomScale currentZoomScale = (CGFloat)(mapView.bounds.size.width / mapView.visibleMapRect.size.width);
// Find out the line width at this zoom scale and outset the updateRect by that amount
CGFloat lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);
updateRect = MKMapRectInset(updateRect, -lineWidth, -lineWidth);
// Ask the overlay view to update just the changed area.
[crumbView setNeedsDisplayInMapRect:updateRect];
}
[self calDistance:initialLocation SecondCor:locationToDraw];
[locationToDraw release];
locationToDraw =nil;
[_bean release];
_bean = nil;
}}
If that the case then, you should try looking into your CrumbPathView.m see if the render function works correctly or not from what i see here you copied & pasted the code from Apple Docs right? In the file you should find this in method drawMapRect:zoomScale:inContext:
if (path != nil)
{
CGContextAddPath(context, path);
CGContextSetRGBStrokeColor(context, 0.0f, 0.0f, 1.0f, 0.6f);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, lineWidth);
CGContextStrokePath(context);
CGPathRelease(path);
}
See if the path is nil or not if it's nil-ed nothing gets rendered. Have you debug into this view? The rendering happens in CrumbPathView so, you should see what happen in it.
If the path is nil then you should check the method which is called to assign its value, located above the if (path != nil)
Sure this is something simple as I'm just starting with the maps. I already have a map showing one location, but when I've added a second anotation the map stays zoomed all the way out rather than going to my locations. The pins are there when I zoom in, so I know that bit's working.
Code snippets:
- (void)viewDidLoad
{
...
...
...
// Set coordinates for our position
CLLocationCoordinate2D location;
location.latitude = [self.lat doubleValue];
location.longitude = [self.lon doubleValue];
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc]
initWithTitle:self.placename
andSubtitle:self.subtitle
andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
[newAnnotation release];
// Set coordinates for our second position
CLLocationCoordinate2D amenitylocation;
amenitylocation.latitude = self.latitude;
amenitylocation.longitude = self.longitude;
// Add the annotation to our map view
MapViewAnnotation *amenityAnnotation = [[MapViewAnnotation alloc]
initWithTitle:self.callouttitle
andSubtitle:self.calloutsubtitle
andCoordinate:amenitylocation];
[self.mapView addAnnotation:amenityAnnotation];
[amenityAnnotation release];
[super viewDidLoad];
}
#pragma mark - MKMapView Delegates
// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
[mv setRegion:region animated:YES];
[mv selectAnnotation:mp animated:YES];
}
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id<MKAnnotation>)annotation
{
if(mapView.userLocation==annotation)
{
return nil;
}
NSString *identifier = #"IDENTIFIER";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(annotationView==nil)
{
annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]autorelease];
annotationView.pinColor=MKPinAnnotationColorPurple;
annotationView.canShowCallout=YES;
}
return annotationView;
}
I'd appreciate any pointers.
Also, am I right in thinking I'll have to make custom callouts if I want more than one to appear on the map at the same time?
Sorry, found the answer - I didn't have the MKMapView delegate linked to File's Owner in IB, although I do have in my header file. Linked that up and it's working.