finding current location in iphone - iphone

trying add annotation on current location when ever i want Any number of annotation at the but it work can any help me out
i try this code
- (CLLocationCoordinate2D)coordinate;
{
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude =MKUserLocation.latitude; //37.786996;// Here we have to change the with current location
theCoordinate.longitude = MKUserLocation.longitude;
return theCoordinate;
}

Make use of CLLocationManagerDelegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

[map setShowsUserLocation:TRUE];
map is the object of MKMapView.

Use this delegate method
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
if (userLocation_) {
[userLocation_ release];
}
userLocation_ = [newLocation retain];
}

Related

Objective-C - Errors when passing a variable into function

What is the easiest way to pass a var into another function ?
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(#"%#", started);
}
I tried:
Defined a global vars:
extern NSString *started;
When I set the NSString directly and passing into another function, it works well:
-(void) startTracking:(CDVInvokedUrlCommand*)command {
started = #"testing";
}
But it doesn't work:
-(void) startTracking:(CDVInvokedUrlCommand*)command {
NSString* myarg = [command.arguments objectAtIndex:0]; // http://docs.phonegap.com/en/2.5.0/guide_plugin-development_ios_index.md.html#Developing%20a%20Plugin%20on%20iOS_writing_an_ios_cordova_plugin
started = myarg;
}
(I'm a objective-C beginner, don't understand it well)
EDIT: Seems like it only crashed when I put the app into background.
Depending on wether you are using ARC, you have to retain the object. You'll probably want to use a property on your class:
in your header:
#property(strong) NSString *started;
in implementation:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(#"%#", self.started);
}
-(void) startTracking:(CDVInvokedUrlCommand*)command {
self.started = #"testing";
}
-(void) startTracking:(CDVInvokedUrlCommand*)command {
NSString* myarg = [command.arguments objectAtIndex:0];
self.started = myarg;
}
Mate, seems like you want to track the date you started receiving location information.
How about doing it like this:
// Your .h file
#interface MyClass <CLLocationManagerDelegate>
{
BOOL hasStartedUpdatingLocation;
NSDate *startDate;
CLLocationManager *locationManager;
}
...
// Your .m file
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// ---------------------------------------------------------------
// if has NOT started updating location, record start date
// otherwise, do not execute this if statement
// ---------------------------------------------------------------
if(!hasStartedUpdatingLocation)
{
hasStartedUpdatingLocation = YES;
// this if statement should only execute once
startDate = [NSDate date]; // get the current date and time
}
else
{
// do something else
}
}

Regarding Map Kit in iOS 6.0.1

I am not able to get my current location in iOS 6.0.1, ![Please see the screen shot when I am trying to get my current location the map view is not showing it just show blank map screen my current location latitude longitude both are correct
My code is as below:-
//Start fetching logged in user current location
self.m_LocationManager = [[CLLocationManager alloc] init];
self.m_LocationManager.distanceFilter = kCLDistanceFilterNone;
self.m_LocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.m_LocationManager.delegate = self;
[self.m_LocationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[manager stopUpdatingLocation];
self.m_LatitudeValue = newLocation.coordinate.latitude;
self.m_LongitudeValue = newLocation.coordinate.longitude;
NSLog(#"lat %f long %f",self.m_LatitudeValue,self.m_LongitudeValue);
}
//for showing current location
- (void)addAnnotations {
MKCoordinateSpan span;
span.latitudeDelta=0.08;
span.longitudeDelta=0.08;
Social_Check_InAppDelegate *appDelegate = (Social_Check_InAppDelegate *)[[UIApplication sharedApplication] delegate];
//user current location
double latitudeValue = appDelegate.m_LatitudeValue;
double longitudeValue = appDelegate.m_LongitudeValue;
CLLocationCoordinate2D coardinate1 = {latitudeValue,longitudeValue};
MKCoordinateRegion region;
region.center=coardinate1;
region.span=span;
CSMapAnnotation *ann = [[CSMapAnnotation alloc]initWithCoordinate:coardinate1];
ann.title = m_PlacesNameString;
ann.type = #"green";
[m_MapView setRegion:region animated:TRUE];
[m_MapView regionThatFits:region];
[m_MapView addAnnotation:ann];
[ann release];
}
Above code for showing cure
this is probably because iOS 6 does not support
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
method. please check the documentation. Above method is deprecated. In iOS 6 and later, the location manager reports events to the locationManager:didUpdateLocations: method of its delegate when they become available.

best way to get changed location iPhone sdk?

My ultimate goal is that i need to send lat and logitude to web server, every time when location changes.
I am using below code for sending lat and longitude of a device to a web server after every 2 minutes but it is not giving sometimes correct latitude and longitude as location changes.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation
{
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
NSLog(#"Location Age%#",[NSString stringWithFormat:#"%d",locationAge]);
if (locationAge > 5) return;
// test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
NSString *stringUrl;
// BOOL check = FALSE;
NSLog(#"Before condition condition");
if(bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy){
NSLog(#"condition");
self.bestEffortAtLocation = newLocation;
if (newLocation.horizontalAccuracy <= locmanager.desiredAccuracy) {
// we have a measurement that meets our requirements, so we can stop updating the location
// IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
[locmanager stopUpdatingLocation];
locmanager.delegate = nil;
}
//check = TRUE;
}
stringUrl = [NSString stringWithFormat:URLSAVELAT,stringUserId,[NSString stringWithFormat:#"%g",self.bestEffortAtLocation.coordinate.latitude],[NSString stringWithFormat:#"%g",self.bestEffortAtLocation.coordinate.longitude]];}
For location manager i am using below code
{
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
locmanager.distanceFilter = 10.0;
//locmanager.distanceFilter = kCLDistanceFilterNone;
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
}
Any,even small help would be appreciated, Thank u in advance
You should use the location update call backs from the API and use updateLocation method:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:#protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationUpdate:newLocation];
}
}
Then in the viewcontroller do this:
- (void)locationUpdate:(CLLocation *)location {
//DO WHATEVER YOU WANT HERE, INCLUDING SENDING TO SERVER
}
You also need to define two protocol methods, one of which is the locationUpdate:
#protocol CoreLocationControllerDelegate
#required
- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;
#end
I would not recommend doing all that you do in the didUpdateLocation: method.

Retrieving Latitude & Longitude CoOrdinates, Displaying in UILabel

I am interested in grabbing my users current Latitude and Longitude coordinates, and displaying them literally as a NSSString in a UILabel on the view.
I don't need any MKMapView or to show anything graphically, just to display the coordinates in a UILabel. Is this possible?
Could anyone provide a starting block for me?
Thanks
Ya, its possible. Just import #import <CoreLocation/CoreLocation.h> and declare the delegate <CLLocationManagerDelegate>. then you can get the values in following delegate mathod.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
CLLocationCoordinate2D location=newLocation.coordinate;
NSString *s = [NSString stringWithFormat:#"%f,%f",location.latitude,location.longitude];
}
There is the CoreLocation framework which does this job. You can get the user's current location by implementing this delegate.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
Follow the steps:
Add MapKit.framework to your project
add to .h #import "CoreLocation/CoreLocation.h" and #import "MapKit/MapKit.h"
Use delegates as, #interface yourInterface : UIViewController < MKMapViewDelegate, CLLocationManagerDelegate >
Now Add following method to your .m file
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self setMapCenter:newLocation.coordinate];
[self._mapView selectAnnotation:[[self._mapView annotations] lastObject] animated:YES];
lblLong.text = [nsstring stringWithFormat:#"%f", newLocation.coordinate.longitude];
lblLat = [nsstring stringWithFormat:#"%f", newLocation.coordinate.latitude];
[self.locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"ERROR");
}
Here mention CLLocationManager *locationManager;. good luck.

Getting magneticHeading value

Can I get the magnetic heading value on the following delegate? If so, how can I get it into a UILabel?
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
}
Yup:
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
myLabel.text = [NSString stringWithFormat:#"%f", [newHeading magneticHeading]];
}