Map view refresh in iPhone app - iphone

I am using map functionality in my iphone app. I m showing stores for users current location on map.
Whenever user scrolls the map he needs to be shown stores of new location. eg. suppose user at
New York at first app will show New York stores but when he scrolls map to Texas then app should fire web service request for Texas location. My problem is
1) if web service request goes at each map scroll, app may crash or wait each time for response for new set of stores. (for this i m going to put some hardcoded radius to send request) So how to handle it proper way.
2) I want to know distance between two location so that i can send request to server only if the distance between 2 locations is greater than some specific value.
I am using map view delegates for above functionality. Please suggest me some proper way to handle it.
Thanks

Well to find the distance between 2 points i use
CLLocation *location1 = [[CLLocation alloc]initWithLatitude:[[dict valueForKey:#"lat"] doubleValue] longitude:[[dict valueForKey:#"lon"]doubleValue]];
float distance =[mUserCurrentLocation distanceFromLocation:location1]/1000;
float distanceinMeters=[mUserCurrentLocation distanceFromLocation:location1]; NSString *distancestr= [NSString stringWithFormat:#"%.2f KM",distance];
See If this can help you.

Related

How can I submit scores with one decimal to Game Center?

I want to send my score with one decimal to Game Center. Ho can I do this??
You can only submit 64 bit integers as scores to a leaderboard. From the documentation:
To Game Center, a score is just a
64-bit integer value reported by your
application. You are free to decide
what a score means, and how your
application calculates it. When you
are ready to add the leaderboard to
your application, you configure
leaderboards on iTunes Connect to tell
Game Center how a score should be
formatted and displayed to the player.
Further, you provide localized strings
so that the scores can be displayed
correctly in different languages. A
key advantage of configuring
leaderboards in iTunes Connect is that
the Game Center application can show
your game’s scores without you having
to write any code.
That doc page should tell you about formatting your score. It sounds like in order to display float-like scores you will have to tinker with the format settings in iTunes Connect.
Try this :
- (IBAction)setScore
{
float score = (float)self.currentScore / 100.0f;
currentScoreLabel.text = [NSString stringWithFormat: #"%f", score];
NSLog(#"%lld", self.currentScore);
}

Getting Altitude(height from sea level) of my location in iphone SDK

I am trying to get Altitude of my location in my application. But every time I try altitude property of CLLocation object I am getting 0.00 as result.
I googled for my query and I got a similar question here and here. This link is saying that if I access CLLocation with wifi or cell tower it will be null. Even if I try to set desireAccuracy to best than even it don't make sure that app will use GPS. It is also saying that if I am indoor that I won't be able to access GPS.
I many cases it its not sure that app will use GPS only. I want some way for getting Altitude from wifi or cell tower too. For that I googled more and I got Google Earth API but I think this is for Microsoft .net technology only.
Now according to this situation I think for a solution I can create a web service in Microsoft Technology and pass my location there and I can get altitude as response but I don't want to do this.
Can anyone suggest me how to get my location's altitude from ios. Is there any way available or not? If yes than please navigate me in right direction.
Thanks in advance.
Edit1
I used CLLocationManager for updating location and when I get my location I need altitude.
Edit2
According to #fishinear's answer I tried following code:
- (void)viewDidLoad
{
[super viewDidLoad];
manager = [[CLLocationManager alloc] init];
[manager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[manager setDistanceFilter:kCLDistanceFilterNone];
[manager setDelegate:self];
[manager startUpdatingLocation];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if ([newLocation verticalAccuracy] >= 0) {
NSLog(#"Altitude = %lf",newLocation.altitude);
}
NSLog(#"Vertical Accuracy : %lf",newLocation.verticalAccuracy);
}
verticalAccuracy is always -1. It is not changing even after running the app to last 30 Mins. I am runnin ios 4.3.2 on my 3GS. I am indoor so I think it is not accessing GPS using this code even.
To answer your original question:
GPS will always be used if you set desiredAccuracy to Best or BestForNavigation, and distanceFilter to kCLDistanceFilterNone when configuring the CLLocationManager. You will then also get altitude values in the CLLocation event.
However, it takes a short while before the GPS has "locked in" to enough satelites to be able to report the altitude. During that time it will report CLLocation events without altitude values. Therefore, you should ignore the first couple of events until the verticalAccuracy value is good enough for you. Also, check the timestamp, because sometimes the first CLLocation event is an old one.
See Android - Get Altitude By Longitude and Latitude? there is an Android solution but it is easily adaptable to iOS.
The answer is to use the USGS Elevation Query Web Service
From the top answer there, basically you just send an HTML Web request to:
http://gisdata.usgs.gov/xmlwebservices2/elevation_service.asmx/getElevation?X_Value=" +
String.valueOf(longitude) +
"&Y_Value=" + String.valueOf(latitude)
+ "&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true";
Then you can parse out the response text from between the <double>...</double> tags
Edit:
It looks like you might be trying to get elevation outside the United States.
Google Maps API does provide an Elevation service and documentation at https://developers.google.com/maps/documentation/javascript/elevation

iPhone:Road Direction Map view

I am showing a map view using MapKit framework in my iPhone application. I am also showing the particular friends list Pin point in map view based on the location they are. I would like to enhance now to showing the road map direction from my location to a particular selected friend location. I know the lat and long for both source and destination, but i have draw route line in road direction, it should be road direction. Could someone help me on this?
Thank you!
MapKit does not expose a means of performing driving directions. So, it's not as simple as asking the map to display a course from location A to location B. You have two options:
1) Integrate with Google's API to get the driving directions, and overlay your own lines onto the MapKit map.
or
2) Simply direct your users out of app and delegate this functionality to the built in Map app.
I have no experience with the former, but the later is very easy. Simply:
CLLocationCoordinate2D location = [[map userLocation] location].coordinate;
double currentLat = location.latitude;
double currentLong = location.longitude;
NSString *googleUrl = [[NSString alloc] initWithFormat:#"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", currentLat, currentLong, item.latitude, item.longitude];
NSLog(#"%#", googleUrl);
[[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:googleUrl]];
Actually there is no api supported by iPhone sdk to draw route on map. There a repo on github which is using google maps api to draw route on map by using map overlay. It has some limitation but you can take help from this repo - https://github.com/kishikawakatsumi/MapKit-Route-Directions

Presenting a walkway from position A to B in my app

So I have an iPhone app which should aid the user to find a convenient walkway from his/her own position to a given destination. As I have learnt, MKMapView does not provide an easy way to infer a preferred walking route from A to B.
I can live with terminating my own app and launch the native map application on the iPhone, but in that case I would like to equip the map application with two coordinates so that the user can find his/her way.
Any suggestions to how I should go about this task?
To launch the native map app use:
NSString *googleMapsURL = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
start.latitude, start.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURL];
where start is the user location and destination is, well, the destination. For walking directions, you can add &dirflg=w (still in beta according to wiki). Here are some more parameters you can use.

Cllocationmanager update oldlocation

when i run my application its get my location(Lat,logi) both new and old location.But i want to set oldlocation as new location when my application run first time.Because i want to calculate distance travel.
If i run my application it takes oldlocation where last location it get (saw distance travel 1 Km when i start application first time)
Hope u Understand
Thanks
You can try to make some flag to indicate the first launching of your app. And then store this flag in NSUserDefaults. For example, if you cannot find your flag after launch, you show alert like "would you like to use your current location as start location?" if user choose YES, so you will update location and store it in the userDefaults. This is a persistent storage. Oh, perhaps, it even will be better to store not flag, but your start location coordinates.