How can I convert CLLocation to CLLocationDegrees, so that I could create CLLocationCoordinate2D structure?
Just get the CLLocationCoordinate2D directly from the coordinate property of CLLocation.
myLocationCoordinate2D = myLocation.coordinate;
Reference Docs
CLLocation.coordinate property will return CLLocationCoordinate2D for you. No need to do any conversions.
Related
i m trying to convert longituude and latitude to NSNumbers to save them in core data.
following my code:
getting coordinate
CLLocationCoordinate2D coordinate = [[mapView.annotations objectAtIndex:0] coordinate];
NSLog(#"Long2: %f", coordinate.longitude);
NSLog(#"Lat2: %f", coordinate.latitude);
Output
2013-04-28 20:22:05.918 [1892:16a03] Long2: -122.406417
2013-04-28 20:22:05.918 [1892:16a03] Lat2: 37.785834
Setting values:
[members.eventDTO setLatLong:coordinate];
- (void)setLatLong: (CLLocationCoordinate2D)coordinate{
self.locLatitude = [NSNumber numberWithDouble:coordinate.latitude];
self.locLongitude = [NSNumber numberWithDouble:coordinate.longitude];
}
then i tried that for output:
NSLog(#"Long2: %f", [members.eventDTO.locLongitude doubleValue]);
NSLog(#"Lat2: %#", members.eventDTO.locLatitude);
2013-04-28 20:22:05.918 [1892:16a03] Long2: 0.000000
2013-04-28 20:22:05.919 [1892:16a03] Lat2: (null)
any clue its returning that?
According to what you described in your question, it seems that the object is still nil after you set it in your setLatLong: method. My hints that I can suggest here are:
try to log the NSNumber inside your setLatLong: method to check if they are being created correctly;
try to log self.locLatitude and self.locLongitude after you set them, to see if they are assigned correctly.
are the NSManagedObject being created correctly?
are you calling save to your NSManagedObjectContext?
Please post more info so we can hunt the problem together ;)
I have set of coordinates saved in array. I am using reverse geocoder
The method i used,
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate altitude:(CLLocationDistance)altitude horizontalAccuracy:(CLLocationAccuracy)hAccuracy verticalAccuracy:(CLLocationAccuracy)vAccuracy timestamp:(NSDate *)timestamp
I need to convert latitude,longitude in CLLocationCoordinate2D.
self.atmLocation.latitude = [tempDict objectForKey:#"Latitude"]; //atmlocation is of type CLLocationCoordinate2D.
This gives me an error "Expression is not assignable". How to solve this? And if this is not possible than please suggest any other way so i can use external set of coordinates.
The tempDict returns an object. Hence you need to convert it.
Try self.atmLocation.latitude = [(NSString *)[tempDict objectForKey:#"Latitude"]doublevalue];
From the documentation
typedef struct {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;
typedef double CLLocationDegrees;
I am using CLLocationCoordinate2D in to assign latitude and longitude like this
CLLocationDegrees latitude = [somelat doubleValue];
CLLocationDegrees longitude = [somelongi doubleValue];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
When i run this code in iPhone simulator its not showing error but wheni run the same application in ios simulator 3.2 that is the iPad simulator its showing me an "Invalid Initializer" error.
How can i resolve this...Please anybody suggest me a solution to this problem
Thanks and regards,
Sankar Chandra Bose
CLLocationCoordinate2DMake is available only on iOS 4.0 and later. Do this rather,
CLLocationCoordinate2D coordinate;
coordinate.longitude = longitude;
coordinate.latitude = latitude;
CLLocationCoordinate2DMake is available only from ios 4.0. Check the reference
CLLocationCoordinate2DMake
Formats a latitude and longitude value
into a coordinate data structure
format.
CLLocationCoordinate2D
> CLLocationCoordinate2DMake(
> CLLocationDegrees latitude,
> CLLocationDegrees longitude)
Parameters
latitude
The latitude for the new coordinate. longitude
The longitude for the new coordinate.
Return Value
A coordinate structure encompassing
the latitude and longitude values.
Availability
Available in iOS 4.0 and later.
I am new at iphone and I have a problem.
I have this code
for (int i=0; i<2; i++) {
Datos *datos = (Datos *)[arr_datos objectAtIndex:i];
CLLocationCoordinate2D coord;
AnnotationItem *annotationItem = [[AnnotationItem alloc] init];
coord.latitude =[datos.latitud doubleValue];
coord.longitude = [datos.longitud doubleValue];
NSLog(#"coord %f",coord.longitude);
NSLog(#"coord %f",coord.latitude);
[annotationItem setCoordinate:coord];
//[annotationItem setEstacion:estacion];
[mapView_ addAnnotation:annotationItem];
[annotationItem release];
}
The problem that it doesn't done anything
But if i change the coord.latitude=40.444 and coord.longitude=-3.700;
this gives me what I want, but I don't want this, because I have an array with many latitudes and longitudes. Can anyone help me with this? when i put coord.longitude=[datos.longitude floatValue];, it doesn't work?
I'm using Xcode 3.2.2
Thanks and forgive me english.
The problem is that i had change the values, I was putting wrong values. Only I have to do is change the
coord.latitude =[datos.longitud doubleValue];
coord.longitude = [datos.latitud doubleValue];
thank everyone for your time.
It seems like Datos is an object you defined since i couldn't find it in the SDK. Given that it could be a few different things:
Either that arr_datos does not have the correct (or any) data inside of it
It could be that the Datos object is incorrectly handling data passed into it and not storing it correctly.
Place a breakpoint in Xcode and verify that arr_datos has the information you think inside of it and that the datos object is correctly storing information.
CLLocationCoordinate2D is not an object so declare it like:
CLLocationCoordinate2D coord;
BTW, you should be getting warnings about CLLocationCoordinate2D *coord - can you check your compiler logs?
[suggestion1]
NSLog(#"datos.lon %#", datos.longitud);
NSLog(#"datos.lat %#", datos.latitud);
[/suggestion1]
[suggestion2]
Note too that you can iterate through all of your datos_arr with the following:
for(Datos *datos in datos_arry) {
NSLog(.....);
}
[/suggestion2]
i am working on a application where i am saving the coordinates of a location in database
now i need to use these coordinates and display the location on a map. Can some one please
suggest me how can i change the latitude & longitude stored as NSString to coordinate. I want
to use these coordinates to display an anotation on the map.
Thanks in advance
CLLocationCoordinate2D anyLocation = [[CLLocationCoordinate2D alloc] init];
anyLocation.latitude = [latText doubleValue];
anyLocation.longitude = [lonText doubleValue];
latText and lonText are strings