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.
Related
I have this:
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
And obtain a NSString like this:
NSString *endPoint =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
and after using this NSString in some context I get this stupid error:
Variable is not a CFString.
But if I create the NSString like this:
endPoint = #"48.8160525,2.3257800" it then works perfect!
For this error Variable is not a CFString I tried the following:
NSString *endPoint1 =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
CFStringRef endPoint =(CFStringRef)endPoint1;
and tried to use endPoint but not working neither this way.Anyone any miraculous idea?Thx
EDIT:partenaire_lat and partenaire_lng are both NSString!!
You have
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
You keep saying that the two variables are NSStrings but you aren't assigning NSStrings to them. You need to assign NSString objects to NSString variables - they aren't created for you automatically.
So the answers which are telling you to use formatted strings are correct. You really should be doing it like this:
partenaire_lat = [[NSString stringWithFormat:#"%f", 48.8160525] retain];
partenaire_lng = [[NSString stringWithFormat:#"%f", 2.3257800] retain];
what are lat and lng? i'm assuming float or double..so you should use [NSString stringWithFormat:#"%f,%f", lat, lng]; (or however you want the floats to be formatted)
You code has several potential problems:
%# format specifier expects object parameter, while it looks like you pass plain float (I may be wrong here as there's not enough context to be sure). Change format to %f to fix your problem if that's really the case:
NSString *endPoint1 =[NSString stringWithFormat:#"%f,%f", partenaire_lat, partenaire_lng];
Your endPoint1 string is autoreleased and may become invalid outside of current scope if you don't retain it. So if you try to use your variable in another method you probably should retain it.
All you need to do
NSString *latStr=[[NSNumber numberWithFloat:partenaire_lat] stringValue];
NSString *lngStr=[[NSNumber numberWithFloat:partenaire_lng] stringValue];
and do whatever you want to do with these two 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=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.
Hey guys, I am working on an application that initially loads a website from the URL http://en.m.wikipedia.org/wiki/::Random? which gives me a random wikipedia page. I was wondering how would I go about parsing the URL to get the actual page label? Like I know that the page for the United States is http://en.m.wikipedia.org/wiki/United_States and Mighty Morphin Power Rangers (the original of course) is http://en.m.wikipedia.org/wiki/Mighty_Morphin_Power_Rangers
How do I go about dealing with the multiple underscores (a random variable) in the URL?
So in the case of your Wikipedia URL, you can use the lastComponent method of NSURL combined with some NSString replacement. Here's an example:
NSURL *url = [NSURL URLWithString:#"http://en.wikipedia.org/wiki/United_Kingdom"];
NSString *title = [url lastPathComponent];
title = [title stringByReplacingOccurrencesOfString:#"_" withString:#" "];
So what happens is you create a NSURL, you ask it for the last component which is returned as a string ("United_Kingdom"), and then you replace all underscores in the string with spaces (giving you "United Kingdom").
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.
I've been modifying some code to work between Mac OS X and iPhone OS.
I came across some code that was using NSURL's URLByAppendingPathComponent: (added in 10.6), which as some may know, isn't available in the iPhone SDK.
My solution to make this code work between OS's is to use
NSString *urlString = [myURL absoluteString];
urlString = [urlString stringByAppendingPathComponent:#"helloworld"];
myURL = [NSURL urlWithString:urlString];
The problem with this is that NSString's stringByAppendingPathComponent: seems to remove one of the /'s from the http:// part of the URL.
Is this intended behaviour or a bug?
Edit
Ok, So I was a bit too quick in asking the question above. I re-read the documentation and it does say:
Note that this method only works with file paths (not, for example, string representations of URLs)
However, it doesn't give any pointers in the right direction for what to do if you need to append a path component to a URL on the iPhone...
I could always just do it manually, adding a /if necessary and the extra string, but I was looking to keep it as close to the original Mac OS X code as possible...
I would implement a myURLByAppendingPathComponent: method on NSURL that does the same thing. The reason to give it a different name is so it doesn't override the Apple-provided method when Apple gets around to porting the 10.6 API to the iPhone (so the "my" is just an example — the point is that it's unlikely somebody else would write a method with that name).
It seems to me you just want to mess with the path rather than the whole URL. Here's an untested example:
- (NSURL *)myURLByAppendingPathComponent:(NSString *)component {
NSString *newPath = [[self path] stringByAppendingPathComponent:component];
return [[[NSURL alloc] initWithScheme: [self scheme]
host: [self host]
path: newPath]
autorelease];
}
It would only work correctly with URLs that have file-like paths, but I'm pretty sure the Apple method works the same way. At any rate, hopefully it helps you in the right direction.
URLByAppendingPathComponent
Since iOS 4, URLByAppendingPathComponent is available on iOS and handles the two slashes correctly. (OS X had it since 10.6., as Chuck points out)
myURL = [myURL URLByAppendingPathComponent:#"hello world"]
// http://foo/bar/hello%20world
Note that unlike stringByAppendingPathComponent, this method escapes the argument.
URLWithString:relativeToURL:
Alternatively, there is URLWithString:relativeToURL:, which does not escape. So if the url component is already escaped, use:
myURL = [NSURL URLWithString:#"hello%20world" relativeToURL:myURL]
// http://foo/bar/hello%20world
Note that myURL needs to end with a slash here and the added segment must not have a leading slash.
The NSString Reference says this about stringByAppendingPathComponent:
Note that this method only works with
file paths (not, for example, string
representations of URLs).
So, I think it's a case of "Don't Do That".
Use -stringByAppendingString: instead?
There is a simple work around.
Using [NSString stringWithFormat:#"%#/%#", server, file] works fine.
E.g. server : ftp://www.server.com and file : file.txt
+ (NSString *)urlStringPathWithComponents:(NSArray *)paths
{
NSString *url = #"";
for( NSString *item in paths)
{
if([item isEqualToString:paths.firstObject])
{
url = [url stringByAppendingString:[NSString stringWithFormat:#"%#", item]];
}else{
url = [url stringByAppendingString:[NSString stringWithFormat:#"/%#", item]];
}
}
return url;
}
As you see the code above is a class method and this permit use in anywhere without a instance of an object.
Note: Always the first object of the array must be a base url.