I created a MAPVIEW to get latitude and longitude of user's current location.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
The didUpdateLocations delegate method gives an array which has current location's latitude and longitude.
How do i get latitude and longitude from that array?
The delegate method will return the array of CLLocation objects. This array contains most recent location at end of array.
So, you should receive it like this:
CLLocation *loc = [locations lastObject];
To access latitude, longitude write below code.
CLLocationDegrees latitude = loc.coordinate.latitude;
CLLocationDegrees longitude = loc.coordinate.longitude;
In the locations array, you will find CLLocation objects. From there, you can get the coordinates like this:
CLLocation *location = [locations objectAtIndex:someIndex]; //You can have more than one update so loop through it.
CLLocationDegrees latitude = location.coordinate.latitude;
CLLocationDegrees longitude = location.coordinate.longitude;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation * currentLocation = [locations lastObject];
NSString * latitude = [NSString stringWithFormat:#"%.6f", currentLocation.coordinate.latitude];
NSString * longitude = [NSString stringWithFormat:#"%.6f", currentLocation.coordinate.longitude];
}
You need to get the last object from the locations array and then grab the lattitude and longitude by the way.
CLLocation *location = [locations lastObject];
CLLocationDegrees lattitude = location.coordinate.latitude;
CLLocationDegrees longitude = location.coordinate.longitude;
You need to implement below method of delegate to get current location of user.
(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
}
Related
UPDATED for iOS9: I am new to iOS, experimenting with CoreLocation to return latitude and longitude values. The following code never executes the log output
-(void)viewWillAppear:(BOOL)animated{
manager = [[CLLocationManager alloc]init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation];
}
Here is my didUpdateToLocation function with is never reached
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(#"Location: %#", locations);
CLLocation *currentLocation = locations.lastObject;
if (currentLocation != nil) {
float latitude = currentLocation.coordinate.latitude;
float longitude = currentLocation.coordinate.longitude;
NSLog(#"dLongitude : %f", longitude);
NSLog(#"dLatitude : %f", latitude);
}
else{ NSLog(#"ERROR");
}
}
CLLocationManager is asynchronous, You should get the latitude and longitude in the delegate method.
locationManager.delegate = self;
Then implement this delegate method:
- locationManager:didUpdateLocations:
1.first of all, you should use you should use a actual device instead of a simulator
2.implement CLLocationManagerDelegate in the interface,like
#interface XXXXX () <CLLocationManagerDelegate>
3.after
locationManager = [[CLLocationManager alloc] init];
add
locationManager.delegate = self;
4.implement the delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
for (CLLocation *location in locations) {
//you can get locations here
}
}
I am trying to get latitude & longitude. I am running following code on actual device but I always get zero for latitude & longitude. I also import library and set delegate also. May I know what is wrong and how to do? My device has ios 8.
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager locationServicesEnabled])
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *str=[[NSString alloc] initWithFormat:#" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(#"%#",str);
you have to get the current location in delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations objectAtIndex:0];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
//code for adjusting pins location when user re-enters its radius
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
//code for adding pin to map
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
}
- (CLRegion*)dictToRegion:(NSDictionary*)dictionary
{
NSString *identifier = [dictionary valueForKey:#"identifier"];
CLLocationDegrees latitude = [[dictionary valueForKey:#"latitude"] doubleValue];
CLLocationDegrees longitude =[[dictionary valueForKey:#"longitude"] doubleValue];
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
CLLocationDistance regionRadius = [[dictionary valueForKey:#"radius"] doubleValue];
if(regionRadius >_locationManager.maximumRegionMonitoringDistance)
{
regionRadius =_locationManager.maximumRegionMonitoringDistance;
}
NSString *version = [[UIDevice currentDevice] systemVersion];
CLRegion * region =nil;
if([version floatValue] >= 7.0f) //for iOS7
{
region = [[CLCircularRegion alloc] initWithCenter:centerCoordinate
radius:regionRadius
identifier:identifier];
}
else // iOS 7 below
{
region = [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
radius:regionRadius
identifier:identifier];
}
return region;
}
So I am trying to make it so that when an iBeacon leaves the region being monitored(which is always the area around the users phone) it also creates a map annotation/pin, thereby marking its last known location. Can anyone help or point me in the right direction?
An iBeacon is in effect a geofence. You define a CLBeaconRegion and then monitor for region entry/exits as you do with CLCircularRegion. Unlike CLCircularRegion, a CLBeaconRegion is defined in terms of the iBeacons UUID and optionally its major and minor values.
You will receive a call to your delegate's didEnterRegion and didExitRegion method when the iBeacon is visible/no longer visible - It is described in the Location and Maps Programming Guide
You don't get the beacon's location, but you can capture the device's GPS location when you exit the region as an approximate location to where the beacon was last seen. Once you have that, then creating a map annotation is trivial.
Hi in my application I'm fetching the the current location using Google IOS SDK know i want to store the current location Longitude and Latitude NSString please tell me how to do it.
My current location fetching code.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
longitude:newLocation.coordinate.longitude
zoom:17.0];
[googleMapView animateToCameraPosition:camera];
}
I'm using the above code to get the current location please tell how to store the latitude and langitude in NSString .
Thanks.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
[locationManager stopUpdatingLocation];
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
longitude:newLocation.coordinate.longitude
zoom:17.0];
[googleMapView animateToCameraPosition:camera];
NSString *getcurrlong = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
NSString *getcurrlat = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
Swift 3
func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation, from oldLocation: CLLocation) {
print("didUpdateToLocation: \(newLocation)")
locationManager.stopUpdatingLocation()
let currentLocation: CLLocation? = newLocation
if currentLocation != nil {
let camera = GMSCameraPosition.camera(with: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude, zoom: 17.0)
googleMapView.animate(to: camera)
var getcurrlong = String(format: "%.8f", currentLocation?.coordinate?.longitude)
var getcurrlat = String(format: "%.8f", currentLocation?.coordinate?.latitude)
}
}
Since I have updated my ios to 7.I dont know how get the current location and altitude with the new method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
Can Any one show me some sample code how to use this method to get current location and altitude?
Thanks a LOT.
The most recent location will be at the end of the locations array.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *mostRecentLocation = [locations lastObject];
bool haveValidAltitude = (mostRecentLocation.verticalAccuracy > 0);
if(haveValidAltitude)
{
NSLog(#"current altitude is: %g meters above sea level", mostRecentLocation.altitude);
}else{
NSLog(#"current altitude is unavailable");
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
NSLog(#"latitude %+.6f, longitude %+.6f\n", location.coordinate.latitude, location.coordinate.longitude);
}
locations is an array, last object of which is the newest available location.