Problem with reading user location into coordinates on iPhone - iphone-sdk-3.0

I have a problem what I suppose is rather trivial...
I have coded an app using the mapkit and have done the development on my Mac using the simulator which has no GPS. Therefore did I hard code the coordinates to simulate a user position. Today as I tried my app on my iPhone for the first time I thought I only had to change my code a bit in order to read the coordinates from the GPS and to get the information about the users position. But it did not work that easy. Does anyone have an idea how to solve this? I need the user longitude and latitude to calculate the distance to another position and I thought I could do something like this...?
CLLocationManager *clm = [[CLLocationManager alloc]init];
[clm.delegate self];
[clm startUpdatingLocation];
userLocationFromGPS = clm.location.coordinate;//the userLocationFromGPS is a CLLocationCoordinate2D userLocationFromGPS object
user = [[Annotations alloc]initWithCoordinate:userLocationFromGPS];
[user setTheTitle:#"You are here!"];
userLocationPlain = [[CLLocation alloc]initWithLatitude:userLocationFromGPS.latitude longitude:userLocationFromGPS.longitude];
I need the coordinates but cant figure out how to get them...any ideas?
Thanks in advance!

CLLocationManager reports location asynchronisly, so you would need to provide a delegate.
Consider going over LocateMe code sample. Also, you should hard-code coordinates in the simulator. The simulator would use the Apple headquarters coordinates.
Here is some refactoring for the code you provided (I didn't run it):
Consider doing the following:
CLLocationManager *clm = [[CLLocationManager alloc] init];
clm.delegate = self;
[clm startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
Annotations *user = [[Annotations alloc] initWithCoordinate:newLocation];
[user setTheTitle:#"You are here!"];
CLLocation *userLocationPlain =
[[CLLocation alloc] initWithLatitude:newLocation.latitude
longitude:newLocation.longitude];
// At some point you should call [manager stopUpdatingLocation]
// and release it
}

As this says, you might need to first import the location framework.

Thank you both for answering!
msaeed:
Your code is looking good. I have defined a delegate but forgot to implement the method as you do in you example. I will try it out! Thanks!
mcandre:
Yes I added the framework and I use the functionalities in it in other methods in my app, when reading and handling position from a KML.
/MACloop

Related

IOS Map user location not displayed

I am experimenting with apple mapkit apple mapkit guide, and i noticed that to show the user location (blue dot), it suffice to set show userlocation to true in storyboard. I am testing on simulator (trying iphone 6 and 7) and i emulate locations using gpx files.
My problem is no blue dot appears on screen. What can be wrong ?
Are you actually starting up the location manager and updating the location?
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
And don't forget to ask the user for permission as well.

Changing the image in UIimageView

Hi I am new to ios programming,
I am trying to change the image of UIImageView outletImage
- (void)viewDidLoad
{
[super viewDidLoad];
dragView *dragViewCopy=[[dragView alloc]init];
newPic=[dragViewCopy imageChanger];
outletImage.image=[UIImage imageNamed:(NSString*)newPic];
NSLog(#"vc %#",newPic);
}
but it is not changing,inspite that the correct name of the image appears when i test it with NSLog
when i try this
outletImage.image=[UIImage imageNamed:(NSString*)#"bird.png];
it works
Can anybody help me about this
thanks
Your problem appears to be that you're trying to load an image from disk (or whatever the iOS device's hard drive equivalent is) and the actual image you have (which comes from the "imageChanger") method is entirely within memory.
So instead of doing "[UIImage imageNamed:]", you may want to create your image using "[UIImage alloc] initWithData:" or use whatever actual object type "imageChanger" returns.

MKPointAnnotation is overlaying my MKMapView

This is the problem and what i'm trying to achieve
http://i.stack.imgur.com/XpDQn.jpg
This is what i tried to achieve my goal
http://i.stack.imgur.com/lF5hy.png
and my didUpdateLocations delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation* location=[locations lastObject];
MKPointAnnotation*annotation=[[MKPointAnnotation alloc]init];
annotation.coordinate=location.coordinate;
annotation.title=#"Me";
annotation.subtitle=#"Current location";
MKCoordinateRegion region;
region.center=location.coordinate;
if(myLocation!=nil){
[_mapAsset removeAnnotation:myLocation];
myLocation=nil;
}
[_mapAsset addAnnotation:annotation];
myLocation=annotation;
[self zoomToFitMapAnnotations:_mapAsset];
}
As you can see in ScreenShot, my MKPointAnnotation is overlaying my MKMapView.I tried to put my mapView in another UIView (to use it like container) but it didn't work. I believe reason why this happening is beacuse I'm putting my annotation in code which means on all layers.
I'm using iOS7.1 SDK and Xcode5. Any advice can be useful.
Thanks in advance
Thanks to #Evan_Mulawski and answer here
I finally did what i tried.
First of all i deleted all of my mapview and its containerview's attributes like setShadowColor,cornerradiues,masksToBounds e.g.
And also deleted what i did in my storyboard to make them fresh new.
Then i added these code
[[_viewContainer layer] setShadowOpacity:0.55f];
[[_viewContainer layer] setShadowRadius:15.0f];
[[_viewContainer layer] setCornerRadius:8.0f];
[[_viewContainer layer] setBorderWidth:1.0f];
and fixed my issue. Thanks for helps

How to remove or hide background in MKMapView when using custom tiles

Having tried many methods I still haven't found a good and full-proof way of preventing the usual "maps" from being shown behind custom map tiles that I am using. Ultimately I want my app to have a map page consisting only of a custom map.
I am really looking for a solution that is pure iOS and doesn't require any 3rd party software but it would appear difficult.
I have tried 3 methods already:
Number 1, hiding the background map via it's view:
NSArray *views = [[[self.mapView subviews] objectAtIndex:0] subviews];
[[views objectAtIndex:0] setHidden:YES];
this however doesn't work on a certain new operating system coming out very soon! The whole screen goes blank. The Apple Developer Forum hasn't provided a solution either
Number 2, Using another blank overlay (e.g. MKCircle) to cover the background map. This works however when scrolling or zooming out quickly, sometimes the overlay flickers off and you can briefly see the background map behind so not ideal.
Number 3, and this is what I have been working on for a few days now is to simply prevent the user from zooming out. Most documented methods tend to use regionDidChangeAnimated or regionWillChangeAnimated, however these do not seem to suddenly stop the map zooming out when pinching - they wait until the pinch movement has finished before taking effect so again it means the background map can be viewed briefly.
So now I am stumped, unless of course I have missed something with these other two methods.
So any help would be much appreciated!
Add this:
-(void)viewDidAppear:(BOOL)animated
{
MKTileOverlay *overlay = [[MKTileOverlay alloc] init];// initWithURLTemplate:tileTemplate];
overlay.canReplaceMapContent = YES;
[map addOverlay:overlay];
overlay = nil;
}
-(void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *, NSError *))result
{
NSData *tile =nil;
}
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay: (id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKTileOverlay class]])
{
MKTileOverlayRenderer *renderer = [[MKTileOverlayRenderer alloc] initWithOverlay:overlay];
[renderer setAlpha:0.5];
return renderer;
}
}
It will replace the map content from the background. It worked very well in my case where I am adding an overlay on the whole map and hiding the real map from the user.
You can't do this in current releases without a third-party library like MapBox. However, the future OS release that you speak of lets you do this.

MKMapView in reverse, when compass mode using in my iphone app project

I am developing an iphone application,in that app i am showing the client Parking places in different locations on MKMapView. Now I need to use compass mode in MKMapView to show the client parking places in particular direction (SE,SW,NE,NW), for that I run this below code.
-(void)updateHeading:(CLHeading *) newHeading
{
NSLog(#"New magnetic heading: %f", newHeading.magneticHeading);
NSLog(#"New true heading: %f", newHeading.trueHeading);
double rotation = newHeading.magneticHeading * 3.14159/-180;
[mapView setTransform:CGAffineTransformMakeRotation(-rotation)];
[[mapView annotations] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
MKAnnotationView * view = [mapView viewForAnnotation:obj];
[view setTransform:CGAffineTransformMakeRotation(rotation)];
}];
}
Everthing works fine in MKMapView, but my MKMapView shows in reverse order, when the device starts rotating, I am facing this problem in ios5 and ios6 also.
NOTE: when I test this app in America, map shows correctly, while I test the app in my location (India) Map turns into reverse.
`
Thanks in advance for any help.
Is the map intended to be centered on the user while rotating? If so you could just set the MKMapView's userTrackingMode to MKUserTrackingModeFollowWithHeading.
If not then how about telling us what you see in magneticHeading, trueHeading and rotation when things go right and when they go wrong.