Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-05-2010, 05:26 PM   #1 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default Address to Lat/Long?

According to this thread:

How can I get the coordinates of any given address using CoreLocation framework and not google Maps API? - Stack Overflow

I can use Core Location to get a GPS coordinate and MapKit to convert GPS coordinates to address...?

What I'm looking for is to take a standard US (if that makes any difference )address (BTW, is there a specific format required?), and display it with a pin in a MapView. Is there any sample code to demonstrate this?

I don't want to get just the current location, but be able to put in any location.
musicwind95 is offline   Reply With Quote
Old 07-05-2010, 07:31 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 14
KoolStar is on a distinguished road
Default

Quote:
Originally Posted by musicwind95 View Post
According to this thread:

How can I get the coordinates of any given address using CoreLocation framework and not google Maps API? - Stack Overflow

I can use Core Location to get a GPS coordinate and MapKit to convert GPS coordinates to address...?

What I'm looking for is to take a standard US (if that makes any difference )address (BTW, is there a specific format required?), and display it with a pin in a MapView. Is there any sample code to demonstrate this?

I don't want to get just the current location, but be able to put in any location.
Your going to want to make a nsurlrequest with the google maps api to geocode address.
KoolStar is offline   Reply With Quote
Old 07-05-2010, 08:13 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default Using the Google Maps API to geocode an address into a lat/long

Quote:
Originally Posted by musicwind95 View Post
According to this thread:

How can I get the coordinates of any given address using CoreLocation framework and not google Maps API? - Stack Overflow

I can use Core Location to get a GPS coordinate and MapKit to convert GPS coordinates to address...?

What I'm looking for is to take a standard US (if that makes any difference )address (BTW, is there a specific format required?), and display it with a pin in a MapView. Is there any sample code to demonstrate this?

I don't want to get just the current location, but be able to put in any location.

As the other poster said, you need to use the google maps web API to do this. The map kit (or an other part of iOS) doesn't support address lookup. The good news is that doing this kind of request is REALLY easy.

The following 2 lines will return a lat/long for an address in theAddress:

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", theAddress];
NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];


The string will look something like this:

200,8,lat,long

Where lat and long are decimal latitude and longitude values. The 200 is a success code, and the 8 is a zoom factor. Treat numbers other than 200 at the beginning as an error, ignore the second number, and take the last 2 as your lat/long. You can use the NSString method componentsSeparatedByString to separate the returned locationString into it's components.

A couple of caveats:

-This uses a form of the Google API that Google deprecated this spring. It may be shut down as soon as a little less than 3 years (Google's docs say that their deprecated APIs will continue to be supported for 3 years from the date of deprecation). That part of the google maps API has been replaced with a new version (which is harder to parse).

-All the google maps APIs are subject to Google's terms of service. You are only supposed to use the results to display addresses on a Google-branded map. The way I read the TOS, using the API to create annotations on a map-kit map meets those terms because the map has the Google logo on it. However, I am not a lawyer, and advise you to read the TOS yourself (or better yet, hire a lawyer to read them and advise you.)

-The code above submits a synchronous request. If Google's host hangs, your code will lock up until the request times out. It would be better to use an NSURLRequest and NSURLConnection to submit an async request, but that's more work. You need to create a couple of objects for each request, teach your code to handle 3 or 4 delegate methods, collect the incoming data, handle success and error completion messages, etc. You also have to write code that either only allows 1 async request at a time, or handles multiple pending requests. That said, I took the easy way out, and used the sync request shown above. In my testing, it works quite well, and only takes a few seconds.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 07-05-2010 at 08:16 PM.
Duncan C is offline   Reply With Quote
Old 07-05-2010, 08:13 PM   #4 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Huh?

Some sample code would be much preferred. Thanks!
musicwind95 is offline   Reply With Quote
Old 07-05-2010, 08:15 PM   #5 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Never mind Duncan, I didn't see your post yet. Thanks for that.

This app is just for a friend, and I don't plan on selling it. Regardless, could you provide some basic info on their new format, just for future-proofing purposes? Thanks!
musicwind95 is offline   Reply With Quote
Old 07-05-2010, 08:18 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by musicwind95 View Post
Never mind Duncan, I didn't see your post yet. Thanks for that.

This app is just for a friend, and I don't plan on selling it. Regardless, could you provide some basic info on their new format, just for future-proofing purposes? Thanks!
I don't have the docs in front of me. If I can track them down I'll post a link. It's a question of wading through a lot of info on Google's site.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-05-2010, 08:22 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Duncan C View Post
I don't have the docs in front of me. If I can track them down I'll post a link. It's a question of wading through a lot of info on Google's site.
I found it. Here a link to the new Google API for geocoding:

The Google Geocoding API - Google Maps API Web Services - Google Code


It uses either JSON or XML.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-06-2010, 08:45 AM   #8 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Is there any specific format that the address has to be in, or is Google flexible?
musicwind95 is offline   Reply With Quote
Old 07-06-2010, 09:05 AM   #9 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by musicwind95 View Post
Is there any specific format that the address has to be in, or is Google flexible?
Iit takes about 5 lines of code total to use it. Try it out and see for yourself!
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-06-2010, 04:29 PM   #10 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

I would think that I would have to convert spaces in the address to %20 character sequences...How would I do that? I'm thinking of using componentsSeparatedByString: on the address, but I'm not sure that method supports plain spaces...But if it did, then I would join those with a stringWithFormat: method, as well as %20s. Would that be right?
musicwind95 is offline   Reply With Quote
Old 07-06-2010, 05:07 PM   #11 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by musicwind95 View Post
I would think that I would have to convert spaces in the address to %20 character sequences...How would I do that? I'm thinking of using componentsSeparatedByString: on the address, but I'm not sure that method supports plain spaces...But if it did, then I would join those with a stringWithFormat: method, as well as %20s. Would that be right?
Just take the address you get from the user and do this:


Code:
theAddress = [addressInput.text  stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

Actually, I just realized that characters like "&" will likely mess up the google geocoding API. I have a routine that parses strings and escapes a set of characters that I specify. It looks something like this (I reworked it slightly to make sense outside of my code library):

Code:
CFStringRef charsToEscape = CFSTR("&=");

- (NSString *)stringByAddingPercentEscapesToString:(NSString*) theString 
{ 
	return [(NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, 
																(CFStringRef) theString, NULL, charsToEscape, 
																CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) autorelease]; 
}
BTW, google is very forgiving on addresses.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-06-2010, 11:38 PM   #12 (permalink)
Senior Member
 
Join Date: Feb 2010
Location: dallas
Posts: 219
gud4nuthin is on a distinguished road
Default

how about to display data based on lat and long

lets say i have lat=1.3 and long=103 so how can i directly display them on map???
gud4nuthin is offline   Reply With Quote
Old 07-07-2010, 12:49 AM   #13 (permalink)
Senior Member
 
Join Date: Feb 2010
Location: dallas
Posts: 219
gud4nuthin is on a distinguished road
Default

i am using this to diplsay map of destination
Code:
	NSString * theAddress = [myString  stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
	NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
						   theAddress];
		
	NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
	
	NSLog(@" GEO MAP  is %@",locationString);
how ever i want to show my current location as well on that map only
i can track my current lat and long using GPS but i dont know how to display them to map if i pass simply like above then i am getting just address from current lat and long

plz help
gud4nuthin is offline   Reply With Quote
Old 09-14-2011, 11:54 PM   #14 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 10
newbee is on a distinguished road
Default Rc

Hello Everyone

I tried using this bit of code for geocoding.

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", theAddress];
NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];

When I did it for the first couple of times, i got the result string correctly.

But after a while it started giving me this error stating : "Registration timer expired, but client is still registering!" and the location string value returned is (null).

When this happens it hangs the app for about 3-5 mins.

Has anybody encountered this before and Any ideas on how to solve this? Pls help.

Thanks In Advance

Quote:
Originally Posted by Duncan C View Post
As the other poster said, you need to use the google maps web API to do this. The map kit (or an other part of iOS) doesn't support address lookup. The good news is that doing this kind of request is REALLY easy.

The following 2 lines will return a lat/long for an address in theAddress:

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", theAddress];
NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];


The string will look something like this:

200,8,lat,long

Where lat and long are decimal latitude and longitude values. The 200 is a success code, and the 8 is a zoom factor. Treat numbers other than 200 at the beginning as an error, ignore the second number, and take the last 2 as your lat/long. You can use the NSString method componentsSeparatedByString to separate the returned locationString into it's components.

A couple of caveats:

-This uses a form of the Google API that Google deprecated this spring. It may be shut down as soon as a little less than 3 years (Google's docs say that their deprecated APIs will continue to be supported for 3 years from the date of deprecation). That part of the google maps API has been replaced with a new version (which is harder to parse).

-All the google maps APIs are subject to Google's terms of service. You are only supposed to use the results to display addresses on a Google-branded map. The way I read the TOS, using the API to create annotations on a map-kit map meets those terms because the map has the Google logo on it. However, I am not a lawyer, and advise you to read the TOS yourself (or better yet, hire a lawyer to read them and advise you.)

-The code above submits a synchronous request. If Google's host hangs, your code will lock up until the request times out. It would be better to use an NSURLRequest and NSURLConnection to submit an async request, but that's more work. You need to create a couple of objects for each request, teach your code to handle 3 or 4 delegate methods, collect the incoming data, handle success and error completion messages, etc. You also have to write code that either only allows 1 async request at a time, or handles multiple pending requests. That said, I took the easy way out, and used the sync request shown above. In my testing, it works quite well, and only takes a few seconds.
newbee is offline   Reply With Quote
Old 03-06-2012, 12:19 AM   #15 (permalink)
Independent Developer
 
Join Date: Nov 2010
Location: Florida
Posts: 1
codemach is on a distinguished road
Default GeoCoding...

Newbee, do you still need assistance with this? I am actually using the newer Google API to accomplish this. I've received the XML string from the Google API, and I'm about to write the XML Parser using the standard Apple xml parsing routines.

Anyway here is the Google site that explains their API:
The Google Geocoding API - Google Maps API Web Services - Google Code

And here is a basic example of the code needed to retrieve the XML containing the Lat & Long. The string locationXML contains the complete string that is ready for parsing.

NSString *strGeoCode = [[NSString alloc] initWithFormat:@"https://maps.googleapis.com/maps/api/geocode/xml?address=9999+NW+1063rd+Ave+Hollywood,+FL&senso r=true"];

NSURL *urlGeoCode = [[NSURL alloc] initWithString:strGeoCode];

NSString *locationXML = [[NSString alloc] initWithContentsOfURL:urlGeoCode];

NSLog(locationXML);

//======== END of Snippet

Note that string strGeoCode has a parameter of "sensor=true" at the end of the string. That parameter is required. It says that the device has a location sensor (GPS or Network). False obviously means that it doesn't. The value also has to be lowercase.

I hope this helps.
codemach is offline   Reply With Quote
Reply

Bookmarks

Tags
address, latitude, location, longitude, mapkit

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 328
7 members and 321 guests
anothermine, Chickenrig, givensur, iNet, michaelhansen, PixelInteractive, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:28 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0