Passing coordinates of location into google places query string - iphone

In my application i am able to get coordinates of my current location. But there is google places string like
"https://maps.googleapis.com/maps/api/place/search/xml?location=52.577798767,-2.124885567&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgydfsaadq37cmcb6fsd80"
Now how can i pass my dynamic coordinates (if i change my location my coordinates would be new) instead of fixed coordinates?
I want to use this string in
NSURL *googlePlacesURL = [NSURL URLWithString:googleUrl];
NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
a and b are
a = [NSString stringWithFormat:#"LATITUDE: %f", location.coordinate.latitude];
//b = [NSString stringWithFormat:#"LONGITUDE: %f", location.coordinate.longitude];

NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgydfsaadq37cmcb6fsd80", longAsFloat, latAsFloat];
It might be hard to see in there, but I added "%f" in place of your long and lat which would allow you to pass them into the string. Then you can build your URL from your new custom string. Hope this helps.

Related

UIlabel into a url?

i created a uilabel that contains my location data using this code,
longitudedata.text = [NSString stringWithFormat:#"longitude=%f", location.coordinate.longitude];
speeddata.text = [NSString stringWithFormat:#"speed=%f", [location speed] * 2.2369];
altitudedata.text = [NSString stringWithFormat:#"altitude=%f", [location altitude]];
but now i need to add these in a url so it will send that data to my server but everything iv tried dosnt work :(
NSURL *myURL = [NSURL URLWithString:[#"http://servername/ldtracker.aspx?id=3&" stringByAppendingString:longitudedata.text]];
and so on but this dosnt work, seems like iv tried everything
im a beginner to xcode, please help :)
UILabels dont handle urls, u need to give them a NSString.
label.text = [NSString stringWithFormat:#"http://servername/ldtracker.aspx?id=3&%#", longitudedata.text];
this should work if i understood ur problem correctly
NSURL *myURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://servername/ldtracker.aspx?id=3&%#&%#%#",longitudedata.text,speeddata.text,altitudedata.text]];

Find all locations in a region through methods of CLGeocoder

It is convenient to use "– reverseGeocodeLocation:completionHandler:" method to Reverse Geocoding a Location. But how to obtain all locations in a region.
ps. If there are several places in a region. How could I use the region information to find out all the places? Such as reverse geocoding a location, given a coordinate, return a location. Here I wanna give a region, return all locations in the region.
There is a google Geocoder API which returns JSON , It is just a kind of a web service which uses GET method
And This is the Google Gecoder API and This is the link for that web service and in this link i have given the region name as london.
Note: You need to include SBJson library to your code.
At the end of that link i have appended address, if you append address- you need to give the region name (or) if you append latitude and longitude, you need to give coordinates and it will return the results accordingly.
And the code for calling google api will be like this
//Call the Google API
NSString *req = [NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%#", esc_addr];
NSLog(#"The get address is %#", req);
//Pass the string to the NSURL
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"The result is %#", result);
//Initialize the SBJSON Class
SBJSON *parser = [[SBJSON alloc] init];
NSError *error = nil;
//Get the resullts and stored in the address array
addressArray = [parser objectWithString:result error:&error];
//Get the latitude values for the given address
NSDictionary *dict = [[[addressArray valueForKey:#"results"] valueForKey:#"geometry"] valueForKey:#"location"];
self.latitudeValue = [[dict valueForKey:#"lat"] objectAtIndex:0];
self.longitudeValue = [[dict valueForKey:#"lng"] objectAtIndex:0];
NSLog(#"LAT : %#",self.latitudeValue);
NSLog(#"LONG : %#",self.longitudeValue);

iPhone application Passing location coordinates into google places string

In my application i am able to get coordinates of my current location. But there is google places string like
"https://maps.googleapis.com/maps/api/place/search/xml?location=52.577798767,-2.124885567&radius=500&types=bank&sensor=false&key=myobfscuredgooglekey"
Now how can i pass my dynamic coordinates (if i change my location my coordinates would be new) instead of fixed coordinates?
i want to use this string in
NSURL *googlePlacesURL = [NSURL URLWithString:googleUrl];
NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
googleUrl
i tried my url like this
NSString *googleUrl=[[NSString alloc]initWithFormat:#"https://maps.googleapis.com/maps/api/place/search/xml?location=%f, %f &radius=500&name=man&sensor=false&key=myobscuredgooglekey",a,b];
a n b are
//a = [NSString stringWithFormat:#"LATITUDE: %f", location.coordinate.latitude];
//b = [NSString stringWithFormat:#"LONGITUDE: %f", location.coordinate.longitude];
NSString *googleUrl = [NSString stringWithFormat:#"/xml?location=%f,%f", location.coordinate.latitude, location.coordinate.longitude];
Use a formatted string using a CLLocation retrieved from your CLLocationManager. (I omitted some text from the URL to make it shorter.)
Evan has the right answer but he left out the left side of your url either for brevity or because you really don't need to format the whole string each time.
NSString *googleUrl = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f", location.coordinate.latitude, location.coordinate.longitude];
You also need to understand that not all coordinate systems speak the same language. Take the exact values you get from location.coordinate.latitute and longituge and manually construct the correct url and see if it works. If Evan's code doesn't work verbatim it's a coordination translation issue. Based on the numbers in the URL I would guess it's in Radians but it could be in Decimal Degrees which is what the iPhone returns.

stringByReplacingOccurrencesOfString not working as expected

Having a problem. Here's my code:
Latitude = [TBXML textForElement:lat]; //Latitude & Longitude are both NSStrings
Longitude= [TBXML textForElement:lon];
NSLog(#"LAT:%# LON:%#",Latitude,Longitude);
NSString *defaultURL = #"http://api.wxbug.net/getLiveWeatherRSS.aspx?ACode=000000000&lat=+&long=-&unittype=1";
newURL = [[defaultURL stringByReplacingOccurrencesOfString:#"+"
withString:Latitude]
stringByReplacingOccurrencesOfString:#"-"
withString:Longitude];
NSLog(#"%#",newURL);
And here's the output:
LAT:-33.92 LON:18.42
http://api.wxbug.net/getLiveWeatherRSS.aspxACode=000000000&lat=18.4233.92&long=18.42&unittype=1
As you can see, something strange is happening to the appending code. Am I doing something wrong here?
Before replacing the longitude, the string is
http://....&lat=-33.92&long=-&...
^ ^
The system sees that there are two -, and thus both of them will be replaced by the latitude.
You should use a more descriptive string to replace with, e.g.
NSString *defaultURL = #"http://....&lat={latitude}&long={longitude}&unittype=1";
newURL = [defaultURL stringByReplacingOccurrencesOfString:#"{latitude}"
withString:Latitude];
newURL = [newURL stringByReplacingOccurrencesOfString:#"{longitude}"
withString:Longitude];
or simply use +stringWithFormat:.
NSString* newURL = [NSString stringWithFormat:#"http://....&lat=%#&long=%#&...",
Latitude, Longitude];
Here's where we started:
url = #"http://...?ACode=000000000&lat=+&long=-&unittype=1"
Latitude = #"-33.92"
Longitude = #"18.42"
Then you replaced all occurrences of #"+" with #"-33.92":
url = #"http://...?ACode=000000000&lat=-33.92&long=-&unittype=1"
Then you replaced all occurrences of #"-" with #"18.42". Note that there are two '-' characters; one after lat= and one after long=. The one after 'lat' is there because the string you pasted in had a - in it.
url = #"http://...?ACode=000000000&lat=18.4233.92&long=18.42&unittype=1"
Thus, your final result.
#KennyTM, BJ Homer, and madmik3 are correct. Your value is getting replaced twice.
However, you should technically be building your URL in a totally different manner:
NSMutableDictionary *query = [NSMutableDictionary dictionary];
[query setObject:#"000000000" forKey:#"ACode"];
[query setObject:Latitude forKey:#"lat"];
[query setObject:Longitude forKey:#"long"];
[query setObject:#"1" forKey:#"unittype"];
NSMutableArray *queryComponents = [NSMutableArray array];
for (NSString *key in query) {
NSString *value = [query objectForKey:key];
key = [key stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
value = [value stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString *component = [NSString stringWithFormat:#"%#=%#", key, value];
[queryComponents addObject:component];
}
NSString *queryString = [components componentsJoinedByString:#"&"];
NSString *fullURLString = [NSString stringWithFormat:#"http://api.wxbug.net/getLiveWeatherRSS.aspx?%#", queryString];
NSURL *newURL = [NSURL URLWithString:fullURLString];
(ignoring the efficacy of -stringByAddingPercentEscapesUsingEncoding: for now)
The reason this is better is that according to the HTTP specification, the keys and values in the query of the URL should be URL encoded. Granted, you're only encoding numbers for simple keys. But if that ever changes, you URL might break. (The flaw with this method is that it only allows a single value per key, and the HTTP spec allows you to specify multiple values. For the sake of simplicity, I've left that out)
There are also some issues on using -stringByAddingPercentEscapesUsingEncoding:. For more information on that, check out Objective-c iPhone percent encode a string?.
Your LAT is negative. So the - gets replaced twice.

Problem with Tiny URL

I am developing Twitter API to my application. In my app, I want to display the tiny url with parsed xml link in UITexField as dynamically. statically, I could able to display tiny url in UITextField, but dynamically I am not able to display the tiny url. Please help me!
Code: statically(Working fine),
NSURL *url = [NSURL URLWithString"http://tinyurl.com/api-create.php?
url=https://gifts.development.xxxxx.edu/xxxx/Welcome.aspx?appealcode=23430"];
NSString *link = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
Dynamically,
NSString * tiny = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php? url=%#", shtUrl];//shtUrl is parsed string
NSURL *url = [NSURL URLWithString:tiny];
NSString *link = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
In above, dynamically app running without warning and error, but url not displayed and when I am checking debugger, url and link both are show nil value. But shtUrl show whole url value as properly.
I tried with all NSURL class and instance methods and String methods also.
In this line of your code:
NSString * tiny = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php? url=%#", shtUrl];
there is a space after the 'api-create.php?'. This will result in a space in the formated string you are creating and will probably result in URLWithString: being unable to parse the url and returning nil.
Remove the extra space (assuming that it is really there and not just a cut-n-paste error) and see if that fixes the problem.
It's also possible that the shtUrl that you are building a tinyurl for contains special characters that would need to be urlencoded (i.e. percent escaped.) Try adding this:
NSString * encodedShtUrl = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)shtUrl,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
kCFStringEncodingUTF8 );
to encode the shtUrl, then use the encodedShtUrl when creating tiny:
NSString * tiny = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php? url=%#", encodedShtUrl];
See http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ for more about the escaping.