I have an app which prompts the user to input an address. Is there a way to check whether or not the address is a valid address, either using Google's Maps or Places APIs or not? Any help would be appreciated. I know it's a vague question but I'm not really sure how to ask it any other way.
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; // addressField is where you enter your address.
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
If address exists you will get coordinates in latitude and longitude. Hope this helps.
A fast way to kill 95% spam profiles was for my company to have a onboard list of us state names and zip codes. If zip code and state did not match, we peomted the user to check again.
Not foolproof at all, but if spammers are the problem, this might help.
Related
In my iPhone app I am using Google API. One thing I have to do is I have a drop down of countries and when I select any country, i need to fetch all the cities of that country. Is it possible with Google API or I have to use any other. Please suggest!
Thanks-
The google Geocoder API 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. you can give you country name
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);
I have just given an idea, you can get all the names in form of Short name, Long name etc in the JSON Response there
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);
I was trying to show the address in google map in my iphone app.
I tried to use
NSString * theAddressString =......;
NSString * query = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#",theAddressString];
NSString * urlString = [query stringByAddingPercetntEscapesUsingEncoding:NSUTF8StringEncoding];
[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString];
Google map can not find some addresses we provided.(Some Japanese or Chinese addresses) But if I saved the address to iPhone contacts. And then pressed
the contact's address link. It will jump to google maps,though google map first alerted that "can not locate the address", after I confirmed the alert message, another view would display the location in the google map or show the address in the nearby.
So iPhone's "Contact" app may use other apis to filter the address string to locate the address or using some king of "fuzzy search".
Does anybody know how do they achieve it?
I really appreciate your help.
Try out the following one and let me about the status of solution.
NSString * theAddressString =......;
NSString *latlong = [[myLatitude stringByAppendingString:#","]stringByAppendingString:myLongitude];
NSString * query = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%##%#",[theAddressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//NSString * urlString = [query stringByAddingPercetntEscapesUsingEncoding:NSUTF8StringEncoding];
[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString];
New Code with give address:
NSString *theAddressString = #"東村山市野口町1-3-49 アマドムス 102, 東京都, 日本";
NSString *urlString=[NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",[theAddressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
float latitude=0.0;
float longitude=0.0;
if([listItems count] >=4 && [ [listItems objectAtIndex:0] isEqualToString:#"200"]){
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else{
NSLog(#"Error");
}
NSString *myLatitude = [NSString stringWithFormat:#"%f",latitude];
NSString *myLongitude = [NSString stringWithFormat:#"%f",longitude];
NSString *latlong = [[myLatitude stringByAppendingString:#","]stringByAppendingString:myLongitude];
NSString * query = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%##%#",[theAddressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:query]];
I have checked the query string with google map and it is showing me the pin in Google map.
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.
I am doing Fconnect in that when a user connects to facebook I get a string like this
{"id":"100001480456987","name":"Vishnu Gupta","first_name":"Vishnu","last_name":"Gupta","link":"http:\/\/www.facebook.com\/profile.php?id=100001480456987","education":[{"school":{"id":"110885222265513","name":"st.joseph"},"type":"High School"}],"gender":"male","email":"vishu.gupta20#gmail.com","timezone":5.5,"locale":"en_US","verified":true,"updated_time":"2010-11-27T10:10:25+0000"}
Now I want to split the id name and email id of the user so that I can store it in my database.
Can someone tell me how to do it????
You don't want to split a string to get those values. Instead, you want to parse the JSON to grab data. I've used this library, it works very well: http://stig.github.com/json-framework/
Hope this helps!
EDIT: Some sample code:
NSDictionary *dict = [responseFromFacebook JSONValue];
NSString *facebookID = [dict objectForKey:#"id"];
NSString *name = [dict objectForKey:#"name"];
NSString *email = [dict objectForKey:#"email"];
this looks like JSON. Some information on JSON handling with Objective C is available at
http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c
Use a JSON parser. See this answer for links to stackoverflow questions about the different JSON libraries available.
Of course I'd also like to mention my own JSON parsing library, JSONKit. At the time of this writing I think it's fair to say that it's the fastest JSON parser out there.
Try this
NSDictionary *dict=[[NSDictionary alloc]init];
string=[string stringByReplacingOccurrencesOfString:#"{" withString:#""];
string=[string stringByReplacingOccurrencesOfString:#"}" withString:#""];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#""];
NSArray *seperated=[string componentsSeparatedByString:#","];
for(int index=0;index<[seperated count];index++)
{
NSArray *sub=[[seperated objectAtIndex:index] componentsSeparatedByString:#":"];
[dict setValue:[sub objectAtIndex:0] forKey:[sub objectAtIndex:1]];
}