Join David Gassner for an in-depth discussion in this video Geocoding an address to get its coordinates, part of Building Mobile Apps with Google Maps Android API v2.
- View Offline
The Google geolocation service is an online service that lets you easily find information about locations. You can pass in a latitude and longitude and get back a matching location. Or you can pass in a city name, a zip code, You won't find the geolocation service listed in the APIs console, instead its available automatically, but there are some limitations you will find their descriptions on this page under usage limits any particular application identified by its default package has a query limit of 2500 requests per day.
If you have a Google maps API for business license your app can perform up to 100,000 requests per day. This documentation is part of the web services docs for the Google geocoding API this web page describes how to use the API, sending in web service requests and getting back JSON or XML formats, but that's not how you use the service in Android. Instead, you'll use this class, Geocoder, which is a member of the Android.location package.
To use this class, you instantiate it, and then call one of these methods, getFromLocation or getFromLocationName, to geocode or reverse geocode a location. In this demonstration, I'm going to use this method, getFromLocationName. I'll pass in a location name provided by the user and get back a list of address objects. I encourage you though to try the other approaches as well. I'm working in a new version of my project, called Geo Code.
I've made a number of changes to the project in this version. In the layout file, I've wrapped my map fragment inside a linear layout. The linear layout contains a nested linear layout. Which has a small data entry form. There's an edit text control and a button. And the map fragment appears below the data entry form. When the user clicks the button, that runs a method called geolocate. Which is already created in the main activity class.
This method calls another method called hide soft keyboard. If the user showed the soft keyboard to do data entry, this will hide it, so that the map is fully visible. Then I have some code to get the value that the user typed in, and I'm outputting that value using a toast message. I'll run the application, and show you what it looks like right now. The user can touch the edit text control, that brings up the soft keyboard, and then they can type in a value.
And, when they press Go that makes the soft keyboard go away, and I'm showing the value that the user typed in. But, I'm not yet actually using that value and finding the matching location, and that's the code that I'm now going to add. I'll go back to the geoLocate method, and place the cursor after the code that gets the value the user entered. I'll create an instance of the Geocoder class. I'll type the name of the class, and press Ctrl+Space to make sure I've added an import.
I'll name the variable gc, and I'll instantiate it with the classes constructor method. There are two versions of the constructor method the simple one were you simply passing the current context and the one that's look how specific. I'll use the simple one, passing in this for the context, now I am already ready to make my search, when you do the search you will get back a Java list, this is a part of the collections of API and Java and the items in the list will be instances of a class called address.
This is also a part of the Android SDK. I'll type the name of the class and press Ctrl+Space to add the input. I'll name this variable list and I'll get its value by calling gc. That's the Geocoder.getFromLocationName. I'll pass in the string that the user entered and then a value of one, meaning for this function, I only want a single address.
If you want to get a list of all known addresses, pass in a larger number. And then you can add a list to let the user choose what they want. For this demo, I'll keep it simple. I'm getting an error on the left, and I'll press Ctrl+1, and that indicates that getfromLocationName might throw an error. I'm going to handle that, by adding a throws declaration to this method. Get from location name and the other methods of the Geocoder can throw exceptions. Next, I'll get the address from the list that's returned, I'll create a new reference variable, data typed is address and named add, and I'll get it's value by calling list.get.
And I'll pass in a value of zero, meaning give me the first and only item in the list. The address class has a bunch of methods that you can call to get various bits of information about the address. First, I'm going to call a method called, getLocality. I'll create a string called locality. And I'll call add.getLocality. And then I'll change my Toast message so that instead of showing the value that the user entered, I'll show the locality that was returned. I'll save those changes and run my application and see how the geocoder is doing.
I'll start by typing London, and pressing the Go button. And I see London returned, but with an uppercase first character. And that's a good indication that the geocoder is working. Next I'll type in New York. I'll press the Go button, and I get back New York in the proper format. But let's try a famous landmark. I'll type in Taj Mahal and press Go, and I get back a value of Agra, which is the locality where the Taj Mahal is.
So I know I'm getting a valuable object back that has a bunch of information. So next I'm going to use this address object and get the latitude and longitude from it. I'll place the cursor after the Toast message, and I'll create a couple of double values. The first will be the latitude, and I'll get its value from add.getLatitude. Than I'll create an lng, or longitude variable, and get it from add.getLongitude.
And than finally I'll use my existing go to location method. I'll call gotoLocation. I'll pass in the latitude and longitude values and then I'll pass in my default zoom constant that I have declared at the top of the code. I'll save the change and now I'm ready to test fully. I'll run the application and when it reloads once again I'll type in London, and then I'll press Go, and that takes me to the London map.
Then I'll type in Taj Mahal, and I'll press Go again, and that takes me to that location. You can also type in zip codes. I'll type in 98122 and that takes me to Capital Hill in Seattle. So I encourage you to try a variety of strings, strings that you might type into Google Maps and see how the geo location API works for you along with a Google Map on an Android device.
Note: An Android device with Android 3.0 (Honeycomb) or higher, and that has Google Play Store installed, is required to use the course exercises. (Kindle Fire and Nook devices do not qualify.) Finished mapping apps can be deployed on Android 2.2 (Froyo) or later.
- Setting up the developer tools
- Adding required permissions
- Getting a Google Maps API key
- Importing and linking the Google Play services
- Setting a map's initial state
- Geocoding an address
- Getting the current location programmatically
- Adding map markers
- Handling marker events
- Drawing lines, polygons, and circles
- Adding maps to existing apps