I have created a uitableview that calculates the distance of a location in the table. I used this code in cell for row at index path.
NSString *lat1 = [object valueForKey:#"Lat"];
NSLog(#"Current Spot Latitude:%#",lat1);
float lat2 = [lat1 floatValue];
NSLog(#"Current Spot Latitude Float:%g", lat2);
NSString *long1 = [object valueForKey:#"Lon"];
NSLog(#"Current Spot Longitude:%#",long1);
float long2 = [long1 floatValue];
NSLog(#"Current Spot Longitude Float:%g", long2);
//Getting current location from NSDictionary
CoreDataTestAppDelegate *appDelegate = (CoreDataTestAppDelegate *) [[UIApplication sharedApplication] delegate];
NSString *locLat = [NSString stringWithFormat:appDelegate.latitude];
float locLat2 = [locLat floatValue];
NSLog(#"Lat: %g",locLat2);
NSString *locLong = [NSString stringWithFormat:appDelegate.longitude];
float locLong2 = [locLong floatValue];
NSLog(#"Long: %g",locLong2);
//Location of selected spot
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
//Current Location
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:locLat2 longitude:locLong2];
double distance = [loc1 getDistanceFrom: loc2] / 1600;
NSMutableString* converted = [NSMutableString stringWithFormat:#"%.1f", distance];
[converted appendString: #" m"];
It works fine apart from a problem i have just discovered where the distance text is duplicated over the top of the detailed text label when you scroll beyond the height of the page.
here's a screen shot of what i mean.
Any ideas why its doing this?
I can't see the screenshot, but I don't see a problem with your code used to create the string. At least if you're using garbage collection - if you're using manual memory management you'd want to retain/release. It also may be an issue in your .xib file with the table configuration. Finally, are you using any custom views or just standard Cocoa controls? Custom controls can sometimes cause drawing/refresh issues.
Related
I am giving lat long to CLLocation in this way.
CLLocation *loc = [[CLLocation alloc]init];
loc.coordinate.latitude = [sLat floatValue];
loc.coordinate.longitude = [sLng floatValue];
Coming from
NSMutableDictionary *locat = [dictLoc valueForKey:#"location"];
sLat = [locat valueForKey:#"lat"];
sLng = [locat valueForKey:#"lng"];
Showing correct values in console, but kills when allocated to cllocation.
Please guide for the above.
Thanks.
you can get the value in CLLocationCoordinate2D object like bellow...
CLLocationCoordinate2D location;
location.latitude = [sLat doubleValue];
location.longitude = [sLng doubleValue];
OR Also try to retain that string like bellow..
sLat = [locat valueForKey:#"lat"];
[sLat retain];
sLng = [locat valueForKey:#"lng"];
[sLng retain];
and then assign it to CLLocation
hope this helpful to you....
I need to parse this string into three different components:
Location: 1|#69.83623|#24.432223|#Cupertino, California
The value is stored in one NSString. I need it in three different strings. One string for latitude, one for longitude and one for location.
Any idea how I can do that?
Thanks!
You can use this method to get an array of different components:
NSArray *bits = [locationString componentsSeparatedByString: #"|#"];
Each item in the NSArray will be an NSString.
Try the following
NSString *location = #"1|#69.83623|#24.432223|#Cupertino, California";
NSArray *components = [location componentsSeparatedByString:#"|#"];
NSLog(#"%#",components);
float latitude = [[components objectAtIndex:1] floatValue];
float longitude = [[components objectAtIndex:2] floatValue];
NSString *loc = [components objectAtIndex:3];
NSString *t = #"Location: 1|#69.83623|#24.432223|#Cupertino, California";
NSArray *k = [t componentsSeparatedByString:#"|"];
NSLog(#"components %#", k);
I have an iphone app which displays a list of businesses and their location on a Google Map using the MKMapView. As a new feature, I am trying to add a "Nearby" feature, whereby it gets the users current location and displays several map annotations on a google map within the vicinity of say 10KM. Currently, the businesses location is stored as an address string in a mysql database, this is then retrieved and geocoded to display it on a google map.
How do I go about doing this? If there is any tutorials around, please point me to one. (I haven't had any luck with finding one).
Thanks in advance!
UPDATE:
I got it partially working - it's only displaying businesses within a 20KM radius which is great, the problem i'm having is that when they open this Nearby Businesses View, it takes quite some time for it to go through each business and check the distance between the user and each individual business. Is there any way to speed this up? Here is my code:, both these methods are called from the viewDidLoad method.
-(void)getMapData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSInteger *bizMax = [[numOfBiz objectAtIndex:0]intValue];
int x = 0;
while(x < bizMax)
{
NSURL *mapURL = [NSURL URLWithString:[NSString stringWithFormat:#"getBizMap-xml.php?biz_id=%#",[bizIDS objectAtIndex:x]]];
NSMutableArray *mapArray = [[NSMutableArray alloc] initWithContentsOfURL:mapURL];
self.mapLocation = mapArray;
self.mapStringLocation = [mapArray objectAtIndex:0];
NSLog(#"Location: %#",mapStringLocation);
[mapArray release];
CLLocationCoordinate2D trueLocation = [self getLocationFromAddressString:mapStringLocation];
AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:trueLocation];
CLLocation *bizLoc = [[CLLocation alloc] initWithLatitude:trueLocation.latitude longitude:trueLocation.longitude];
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:nearbyMapView.userLocation.coordinate.latitude longitude:nearbyMapView.userLocation.coordinate.longitude];
//double distance = [userLocation getDistanceFrom: bizLoc] / 1000;
double kilometers = [userLocation distanceFromLocation:bizLoc] / 1000;
NSLog(#"Distance from %# to current location is %g",mapStringLocation,kilometers);
if(kilometers < maxDistance)
{
[self.nearbyMapView addAnnotation:addAnnotation];
[addAnnotation release];
}
x++;
}
[pool release];
}
-(void)getNumOfBusinesses
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSString *numOfBizJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"numOfBiz.php"]]];
NSString *bizIDSJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"allBizIDSJSON.php"]]];
SBJsonParser *parser = [[SBJsonParser alloc]init];
numOfBiz = [[parser objectWithString:numOfBizJSON error:nil]copy];
bizIDS = [[parser objectWithString:bizIDSJSON error:nil]copy];
NSLog(#"biz id 1 = %#",[bizIDS objectAtIndex:0]);
[parser release];
[numOfBizJSON release];
[bizIDSJSON release];
[pool release];
}
This isn't that hard. You can fetch all your data, calculate the distance between you and the location. The next step would be comparing the distance with the maxDistance(the search radius). If the distance < maxDistance, add you can display that location.
I think this could be achieved with very little cocoa knowledge...
If you need something more specific, start coding and I'll help you with that.
I have a plist with dictionary of array's with coordinates (stored as strings).
I want to create a CLLocationCoordinate2D from every array and crate an overlay for the map.
I did that -
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"Roots" ofType:#"plist"];
NSDictionary *pointsDic = [[NSDictionary alloc] initWithContentsOfFile:thePath];
NSArray *pointsArray = [NSArray arrayWithArray:[pointsDic objectForKey:#"roade1"]];
CLLocationCoordinate2D pointsToUse[256];
for(int i = 0; i < 256; i++) {
CGPoint p = CGPointFromString([pointsArray objectAtIndex:i]);
pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
NSLog(#"coord %f",pointsToUse [i].longitude);
NSLog(#"coord %f",pointsToUse [i].latitude);
}
MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse count:256];
[[self mv] addOverlay:myPolyline];
but the app is crashing without any error.
(BTW when i remove the addOverLay method the app does not crash).
I have 2 questions-
What am i doing wrong?
I have tried to set the pointsArray count as the argument for the CLLocationCoordinate2D like that -
CLLocationCoordinate2D pointsToUse[pointsArray count];
And i am getting an error.
How can i set the CLLocationCoordinate2D dynamically ?
Thanks for any help.
Shani
O.K
The problem was indeed in the viewForOverlay method (thanks aBitObvious and all the rest).
It appears that the line loading of the point from the array is working good.
and for the second question i just separated it to 2 steps:
NSInteger c = [pointsArray count];
CLLocationCoordinate2D pointsToUse[c];
and it worked fine, so if any one is looking for a way to load overlayes from plist, that way is working for me.
Thanks
shani
i want to give option to user to put the location name what he want to search in google map from iphone. when user put the location name that particular map should come.
for now i am doing this by fixing the value of the of coordiante in its object latitude and longitude.
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location.latitude=19.14;
location.longitude=73.10;
is there any way to give the value of this coordinate in text and convert this to the value of CLLocationCoordinate2D ?
I am not entirely clear on your question, but if you are wanting to convert an NSString into a CLLocationCoordinate2D you can use the following:
{
[self useLocationString:#"19.14,73.10"];
}
- (void) useLocationString:(NSString*)loc
{
// the location object that we want to initialize based on the string
CLLocationCoordinate2D location;
// split the string by comma
NSArray * locationArray = [loc componentsSeparatedByString: #","];
// set our latitude and longitude based on the two chunks in the string
location.latitude = [[[NSNumber alloc] initWithDouble:[[locationArray objectAtIndex:0] doubleValue]] autorelease];
location.longitude = [[[NSNumber alloc] initWithDouble:[[locationArray objectAtIndex:1] doubleValue]] autorelease];
// do something with the location
}
This code doesn't check the validity of the string, which you could do be checking the NSArray you get back from componentsSeparatedByString.
Here's a more modern approach.
- (CLLocationCoordinate2D) get2DCoordFromString:(NSString*)coordString
{
CLLocationCoordinate2D location;
NSArray *coordArray = [coordString componentsSeparatedByString: #","];
location.latitude = ((NSNumber *)coordArray[0]).doubleValue;
location.longitude = ((NSNumber *)coordArray[1]).doubleValue;
return location;
}