CLLocationCoordinate2D converting return null - iphone

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 ;)

Related

How to give coordinates from variable

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;

Get coordinates by giving name of place in iphone

Is it possible to get coordinates by giving name of a place in iPhone? I don't want to use any Web Service as they have few limitations. Can i do it using iPhone SDK?
Thanks in advance.
As #raj2raaz have mentioned, we can'nt get coordinates by just specify the name of the place, you must have to use web servics in iPhone to get coordinates.
http://iphonesdksnippets.com/post/2010/02/15/Get-Coordinates-from-Address.aspx
The short answer is no, you can't give an address and get the longitude / latitude position. Several people have written libraries that use the different web services, see this answer for details: Forward geocoding from the iPhone .
I know that you said you don't want to use various web services, but, well, you can't get everything for free. Somebody's CPU cycles are going to have to do a search. Most of them seem to me to have terms that are acceptable for most applications.
Yes, it's possible, but you have to do some work. I am currently putting this effort in one of my project. The GeoNames geographical database covers all countries and contains over eight million placenames that are available for download free of charge. I am adding their cites with population over 1,000 3.9M zip file to my project. It's contain long/lat of each city. I am going to parse each city into a custom NSObject and load them into Core Data.
In my project, instead of doing a city name search, I am going to find the closet city to a particular lat/long coordinate. Haversine formula is used to calculate the distance between two points on a sphere. Here are the formula written in Objective-C and Perl.
My current progress of parsing that data can be found here. I still have work to complete.
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:YOUR_LOCATION completionHandler:^(NSArray* placemarks, NSError* error)
{
NSLog(#"completed");
if ( error )
{
NSLog(#"error = %#", error );
}
else
{
//Here you get information about the place in placemarks array
}
}];
you can get the coordinates by calling this method .this method requires the address(name of place).
-(CLLocationCoordinate2D) addressLocation:(NSString *)addrss {
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[addrss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}

Iphone CLLocationCoordinate2D

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]

EXC_BAD_ACCESS signal received

I am getting a EXC_BAD_ACCESS signal when calling the following line:
self.distance = [NSNumber numberWithDouble:[currentLocation distanceFromLocation: self.location]];
This is only happening in iOS 3.2 for iPad,
I know this is a memory issue but i can't seem to see what is wrong with the above line?
edit: here is the full method:
-(void)updateDistance:(CLLocation *)currentLocation {
self.distance = [NSNumber numberWithDouble:[currentLocation distanceFromLocation:self.location]];
placeWrapper.distance = self.distance;
}
which is called like so:
[place updateDistance:self.currentLocation];
self.currentLocation is created here:
CLLocation *location = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
self.currentLocation = location;
[location release];
Another edit :)
here is the stack trace: http://pastie.org/1222992
Run your code with NSZombieEnabled set. This should tell you if you are over releasing or under retaining somewhere.
It's difficult to say without demonstrating where/how you're creating "currentLocation", "location", or possibly even "self". I'm guessing either currentLocation or self.location are not properly retained on creation/setting.
You need to retain something...
[currentLocation retain]
or
[self.location retain];
but you have to do it further up the code. Something's getting "forgotten" or goes "out of scope" so try those retains.
DON'T FORGET TO RELEASE WHATEVER IT IS THAT YOU'RE RETAINING.

Storing and retrieving CGPoints inside NSMutableArray

I've looked through countless questions on here and elsewhere and cannot for the life of me figure out what I'm doing wrong.
I'm trying to store an array of CGPoints as NSValues inside of an NSMutableArray named points like so on the iPhone:
NSValue *point = [NSValue valueWithCGPoint:firstTouch];
NSLog(#"NSValue *point = %#", point);
[points addObject:point];
NSLOG OUTPUT
NSValue *point = NSPoint: {120, 221}
Everything is going smooth converting from the CGPoint to NSValue. But when I try to retrieve the point I get nothing.
NSValue *getPoint = [points objectAtIndex:0];
CGPoint thePoint = [getPoint CGPointValue];
NSLog(#"Point = %#", NSStringFromCGPoint(thePoint));
NSLOG OUTPUT
Point = {0, 0}
The points should be the same but I'm getting a null result.
For testing purposes this is happening in the touchesBegan method.
Does anyone have any idea where I'm going wrong? Thanks in advance.
I never allocated my array into memory and initialized it. My points weren't being stored into an array because there was no array that existed.
for adding CGPoint to NSMutableArray
[ArrObj addObject:NSStringFromCGPoint(PointObj)];
for getting CGPoint from NSMutableArray
CGPoint pointObj2 = CGPointFromString([ArrObj objectAtIndex:index]);
may this help you..
First allocate your mutable array in memory:
points = [[NSMutableArray alloc] init];
Then insert.