to get float values of latitude and longitude from database - iphone

I am trying to get the latitude and longitude of certain regions stored in my database and to store them in an array. I have to store the values as float types. I am getting other values by the following code:
NSString *walkQuery = [[NSString alloc] initWithFormat:#"SELECT Wid,WName,SName,Latitude,Longitude from Walks,SubRegions WHERE Walks.Sid=SubRegions.Sid AND Rid = %d",[regionId integerValue] ];
sqlite3_stmt *walkstatement = nil;
if (sqlite3_prepare_v2(walkNameDB,[walkQuery UTF8String], -1, &walkstatement, nil) == SQLITE_OK)
{
while( sqlite3_step(walkstatement) == SQLITE_ROW )
{
NSNumber *WId;
int temp1 = (int)sqlite3_column_int(walkstatement, 0);
WId = [[NSNumber alloc] initWithInt:temp1];
char *WNameCharacter;
WNameCharacter = (char *) sqlite3_column_text(walkstatement, 1);
NSString *WNameString = [[NSString alloc] initWithUTF8String:WNameCharacter];
char *SNameCharacter;
SNameCharacter = (char *) sqlite3_column_text(walkstatement, 2);
NSString *SNameString = [[NSString alloc] initWithUTF8String:SNameCharacter];
NSMutableDictionary *tempWalk = [[NSMutableDictionary alloc] init];
[tempWalk setObject:WId forKey:#"WalkId"];
[tempWalk setObject:WNameString forKey:#"WalkName"];
[tempWalk setObject:SNameString forKey:#"SubRegionName"];
[regionWalkArray addObject:tempWalk];
Is their any way by which I can get the two float values of latitude and longitude also like I am doing in my code?
Please suggest....

You can use:
float latitude = (float) sqlite3_column_double(walkstatement, 3);
float longitude = (float) sqlite3_column_double(walkstatement, 4);
And can store in dictionary like:
[tempWalk setObject:[NSNumber numberWithFloat:latitude] forKey:#"latitude"];
[tempWalk setObject:[NSNumber numberWithFloat:longitude] forKey:#"longitude"];
You can retrieve it like:
float latitude = [[tempWalk objectForKey:#"latitude"] floatValue];
float longitude = [[tempWalk objectForKey:#"longitude"] floatValue];

Related

How to add elements to an array created by a 'for' loop ?

I am using for loop for get distance from current location to destination location. I would like an array that contains all distances from current to destination location.
for (i = 0; i < [Arr_Lat count]; i++)
{
NSString *latst1 = [[NSString alloc]initWithFormat:[Arr_Lat objectAtIndex:i]];
NSString *longst1 = [[NSString alloc]initWithFormat:[Arr_Long objectAtIndex:i]];
NSLog(#"First lat : %#",latst1);
NSLog(#"First ong : %#",longst1);
double Doblat1 = [latst1 doubleValue];
double Doblong1 = [longst1 doubleValue];
CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(coord1.latitude = Doblat1, coord1.longitude = Doblat1);
NSLog(#" Coordinat ==== : %f -- %f",coord1.latitude,coord1.longitude) ;
CLLocation *currLoc = [[CLLocation alloc] initWithLatitude:appDel.curr_lat longitude:appDel.curr_long];
CLLocation *destLoc1 = [[CLLocation alloc] initWithLatitude:Doblat1 longitude:Doblong1];
NSLog(#" Currennt Location : %#", currLoc);
NSLog(#" Destination Location : %#" , destLoc1);
distance = [destLoc1 distanceFromLocation:currLoc];
NSLog(#" Distance : ------- %0.3f", distance);
DistStr = [[NSString alloc]initWithFormat:#" %f",distance];
[currLoc release];
[destLoc1 release];
[Arr_title retain];
[tbl_nearplace reloadData];
}
If you want to store the distance you need an NSMutableArray.
Declare a NSMutableArray *distanceArray; in class scope.
Initialize it: distanceArray = [[NSMutableArray alloc] init];
In the for loop after DistStr =[[NSString alloc]initWithFormat:#" %f",distance];
write the following code:
[distanceArray addObject:DistStr];
Say we have this array with stuff we want to iterate over and add those items to another array
NSArray *someArrayWithStuff = #[#"Hello",
#"Its",
#"Very",
#"Cold",
#"Outside",
#"Today"];
Say we want the content of someArrayWithStuff to be added to this other array so we create an NSMutableArray
NSMutableArray *theNewArrayWithOurStuff = [NSMutableArray array];
We loop through the someArrayWithStuff
for (int i = 0; i < someArrayWithStuff.count; i++) {
// Add object to the new array
[theNewArrayWithOurStuff addObject:[someArrayWithStuff objectAtIndex:i]];
}
NSMutableArray *Distance=[[NSMutableArray alloc]Init];
for (i = 0; i < [Arr_Lat count]; i++)
{
NSString *latst1 = [[NSString alloc]initWithFormat:[Arr_Lat objectAtIndex:i]];
NSString *longst1 = [[NSString alloc]initWithFormat:[Arr_Long objectAtIndex:i]];
NSLog(#"First lat : %#",latst1);
NSLog(#"First ong : %#",longst1);
double Doblat1 = [latst1 doubleValue];
double Doblong1 = [longst1 doubleValue];
CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(coord1.latitude = Doblat1, coord1.longitude = Doblat1);
NSLog(#" Coordinat ==== : %f -- %f",coord1.latitude,coord1.longitude) ;
CLLocation *currLoc = [[CLLocation alloc] initWithLatitude:appDel.curr_lat longitude:appDel.curr_long];
CLLocation *destLoc1 = [[CLLocation alloc] initWithLatitude:Doblat1 longitude:Doblong1];
NSLog(#" Currennt Location : %#", currLoc);
NSLog(#" Destination Location : %#" , destLoc1);
distance = [destLoc1 distanceFromLocation:currLoc];
NSLog(#" Distance : ------- %0.3f", distance);
NSString *DistStr = [[NSString alloc]initWithFormat:#" %f",distance];
[Distance addObject:DistStr];
[DistStr release];
[currLoc release];
[destLoc1 release];
[Arr_title retain];
[tbl_nearplace reloadData];
}

Value Stored to during its initialization is never read

float latitude = [((IPADAppDelegate *)[UIApplication sharedApplication].delegate).detailViewController.userStoreInfoObj.StoreLatitude floatValue];
float longitude = [((IPADAppDelegate *)[UIApplication sharedApplication].delegate).detailViewController.userStoreInfoObj.StoreLongitude floatValue];
NSString *strAddress = [((IPADAppDelegate *)[UIApplication sharedApplication].delegate).detailViewController.userStoreInfoObj StoreAddress];
NSString *strCountry= [((IPADAppDelegate *)[UIApplication sharedApplication].delegate).detailViewController.userStoreInfoObj StoreCounty];
NSString *strCode = [((IPADAppDelegate *)[UIApplication sharedApplication].delegate).detailViewController.userStoreInfoObj StoreZip];
if(storeData)
{
latitude = [storeInfoObj.StoreLatitude floatValue];
longitude = [storeInfoObj.StoreLongitude floatValue];
strAddress = [storeInfoObj StoreAddress];
strCountry = [storeInfoObj StoreCounty];
strCode = [storeInfoObj StoreZip];
}
Value stored to latitude during its initialization is never read.
Can anyone help me understand why this is happening?
What can I do to fix this? Please help me out [I try my luck].
# Thanks in advance
one example: the static analyzer has likely detected that storeData will always evaluate to true:
StoreData * storeData = thing.storeData;
if (!storeData) {
/* get out of here!!! */
return;
}
IPADAppDelegate * appDelegate = (IPADAppDelegate*)[UIApplication sharedApplication].delegate;
UserStoreInfoObj * userStoreInfoObj = appDelegate.detailViewController.userStoreInfoObj;
float latitude = [userStoreInfoObj.StoreLatitude floatValue];
float longitude = [userStoreInfoObj.StoreLongitude floatValue];
NSString *strAddress = [userStoreInfoObj StoreAddress];
NSString *strCountry= [userStoreInfoObj StoreCounty];
NSString *strCode = [userStoreInfoObj StoreZip];
if (storeData) << would be redundant and always true
{
latitude = [storeInfoObj.StoreLatitude floatValue]; << why not initialize latitude using this value???
longitude = [storeInfoObj.StoreLongitude floatValue];
strAddress = [storeInfoObj StoreAddress];
strCountry = [storeInfoObj StoreCounty];
strCode = [storeInfoObj StoreZip];
}
but you can significantly reduce the complexity of this program (to read, maintain, and execute) by using something like this:
UserStoreInfoObj * storeInfo = nil;
if (storeData) storeInfo = storeInfoObj;
else storeInfo = ((IPADAppDelegate *)[UIApplication sharedApplication].delegate).detailViewController.userStoreInfoObj;
float latitude = [storeInfo.StoreLatitude floatValue];
float longitude = [storeInfo.StoreLongitude floatValue];
NSString *strAddress = [storeInfo StoreAddress];
NSString *strCountry= [storeInfo StoreCounty];
NSString *strCode = [storeInfo StoreZip];

distance from CurrentLocation to data from Plist File

I have a long list of places with Latitudes and Longitudes in a plist. I want to show a tableView of the places only within X distance of the user's current location. Is there a way to create objects from the lats & longs in the plist file so I can use 'distanceFromLocation'? More importantly, how do I get the array to only display the names with a distance from current less than X? I'm assuming I would need to make a series of objects from lats & longs in the plist, then do an objects in array if objects distanceFrom is less than X, correct?
Please help.
Here's where I am now: I get an error on the double clubLatitude line
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *clubArray = [NSArray arrayWithObjects:[self danceClubLocation], nil];
self.tableData = clubArray;
}
-(CLLocation *)danceClubLocation
{
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];
NSEnumerator *e = [array objectEnumerator];
id object;
while ((object = [e nextObject])) {
double clubLatitude = [[array valueForKey:#"Latitude"] doubleValue];
double clubLongitude = [[array valueForKey:#"Longitude"] doubleValue];
CLLocation *clubLocation = [[CLLocation alloc] initWithLatitude:clubLatitude longitude:clubLongitude];
if ([clubLocation distanceFromLocation:myLocation]<=50) {
return clubLocation;
}
else return nil;
}
return nil;
}
-(CLLocation *)myLocation
{
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude];
NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude];
double myLatitudeD = [myLatitude doubleValue];
double myLongitudeD = [myLongitude doubleValue];
myLocation = [[CLLocation alloc]initWithLatitude:myLatitudeD longitude:myLongitudeD];
return myLocation;
}
As #DavidNeiss said, you have to iterate over the list (an NSArray with the plist as source) and it would be something like this:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"latlong" ofType:#"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];
NSEnumerator *e = [array objectEnumerator];
id object;
while (object = [e nextObject]) {
// do something with object
}
Then you can do what you want (removing what's far from the user or whatever).
Read in your plist, iterate over it to pull out ones within X distance and populate any array with them that will be the data source for your table view?

retrieving float values using sqlite3_column_double

I'm relatively new to iPhone programming so I apologize if this seems a simple question. I have latitude/location columns in my table (FLOAT type) that I am trying to retrieve from my SQLite database.
I can retrieve my character/text based db columns, but don't know how to retrieve my float values. Should I be formatting the attributes using NSString or something else? I wish to use the float values further downstream with CLLocationDistance.
// the array of locations that we will create
NSMutableArray *locations = [[[NSMutableArray alloc] init] autorelease];
const char *sql = "SELECT locations.name, \
locations.address, \
locations.city, \
locations.state, \
locations.phone, \
locations.zip, \
locations.latitude, \
locations.longitude \
FROM locations \
WHERE category = ?1";
// the sqlite statement object that will hold our result set
sqlite3_stmt *statement;
// prepare the statement to compile the SQL query into byte-code
int sqlResult = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);
// reset query before execution
sqlite3_reset(statement);
// set bind variable
sqlite3_bind_text(statement, 1, [categoryParameter UTF8String], -1, SQLITE_TRANSIENT);
if ( sqlResult== SQLITE_OK) {
// step through the results - once for each row
while (sqlite3_step(statement) == SQLITE_ROW) {
// allocate a location object to add to the pharmacies array
Location *location = [[Location alloc] init];
// the second parameter is the column index (0 based) in
// the result set
char *name = (char *)sqlite3_column_text(statement, 0);
char *address = (char *)sqlite3_column_text(statement, 1);
char *city = (char *)sqlite3_column_text(statement, 2);
char *state = (char *)sqlite3_column_text(statement, 3);
char *phone = (char *)sqlite3_column_text(statement, 4);
char *zipcode = (char *)sqlite3_column_text(statement, 5);
float latitude = (float) sqlite3_column_double(statement, 6);
float longitude = (float) sqlite3_column_double(statement, 7);
// set all attributes of the location
location.name = (name) ? [NSString stringWithUTF8String:name] : #"";
location.address = (address) ? [NSString stringWithUTF8String:address] : #"";
location.city = (city) ? [NSString stringWithUTF8String:city] : #"";
location.state = (state) ? [NSString stringWithUTF8String:state] : #"";
location.phone = (phone) ? [NSString stringWithUTF8String:phone] : #"";
location.zipcode = (zipcode) ? [NSString stringWithUTF8String:zipcode] : #"";
[locations addObject:location];
[location release];
}
// finalize the statement to release its resources
sqlite3_finalize(statement);
Are the latitude and longitude stored as degrees in the table? Does your Location object have a CLLocationCoordinate2D property called, say, coordinates to put the coordinates in?
If yes, just set the values directly because CLLocationDegrees is actually just a double. For example:
CLLocationCoordinate2D coords;
coords.latitude = sqlite3_column_double(statement, 6);
coords.longitude = sqlite3_column_double(statement, 7);
location.coordinates = coords;

NSString: fastes way of splitting

my question is a performance question.
I need to split a huge NSString into coordinates. The string has this format:
#"coordx,coordy coordx,coordy coordx,coordy (...)"
My method to parse this is:
-(NSMutableArray*) parsePath:(NSString*) pathString
{
// first split between the coordinates
NSArray* path = [pathString componentsSeparatedByString:#" "];
NSMutableArray* coords = [[NSMutableArray alloc] init];
static NSString *seperator = #",";
NSArray *coord;
for(NSString *coordString in path)
{
coord = [coordString componentsSeparatedByString:seperator];
Coordinate *c = [[Coordinate alloc]init];
c.x = [[coord objectAtIndex:0] intValue];
c.y = [[coord objectAtIndex:1] intValue];
[coords addObject:c];
[c release];
}
return coords;
}
So, is there any way to make this faster?
Thanks!
Sebastian
Using NSScanner is probably faster.
http://developer.apple.com/iphone/library/documentation/cocoa/reference/Foundation/Classes/NSScanner_Class/Reference/Reference.html