Understand how to invoke the PlacePicker user experience to give the user of an app a concise, common way of selecting a place from a list of nearby places. Also see how to constrain the viewport of the PlacePicker to a specific area using Latitude and Lo
- View Offline
- [Voiceover] We've already seen how to determine the most likely place that a user of your app is in but sometimes what you want to do is give the user a nice UI to pick a place from. That's where the Place Picker comes in. It provides apps with a standard experience that they can show to users to choose places from. By default the picker is centered on the user's current location but the developer can specify a set of coordinate boundaries to set the map to. In this exercise we're going to use the Place Picker to display a list of places along with a map that the user can choose a place from.
Let's jump over to the code and see how this works. Here in Android studio I'm going to open up the Place Picker project, which is in chapter five. We'll let that settle down for a moment. In the project explorer let's open the manifest file and there is our permission for accessing file location and remember that you need to put your API key here from the developer council project. I've got mine here and it's blurred out, but you’ll need to replace this with yours.
Let's go ahead and open the layout for the main activity, and let's put it in design view. You can see it's pretty simple, just a button along with a text view to hold some place information. Let's open the main activity, Java file, seal the code. Let's take a look at the code, and if we scroll down you’ll see that the usually place services functions are down here, and if we scroll up a little bit to the onCreate function, the API client is created using GEO DATA API and PLACE DETECTION.
There is also an event handler that fires the pickAPlace function when the user clicks the button in our screen. What we're going to do is scroll on up to pickAPlace. Remember on API level 23, that's Android six and above, the user is going to be prompted for their location permission, so we had to make sure that we have that permission. Let's start some more. What we're going to do is invoke the Place Picker to choose a place that's centered on the user's location.
What we're going to do is create a Place Picker intent builder, and this is what I'm gonna say, PlacePicker.IntentBuilder equals new PlacePicker.IntentBuilder, and now we need to create the intent. I'll write, intent intent equals builder.build, and we'll pass in this activity. We need to fire off the intent using start activity for result, and this will show the picker and you can see that we have that code right here.
When that activity resolves it will call the on activity result function and what we're doing is we're checking to see if a place was selected. If the code that's passed in is the Place Picker request, and the result code is result okay, then we call the getPlace function on the Place Picker class and we pass in the intent data. Then we update the UI using the updateUI function with the chosen place.
updateUI is a function which is right here and it updates the text view with some selected data from the place, the name, address, phone number, the website, and so on. Let's go ahead and run what we have. I've already got my emulator running, so I'm just gonna click on the debug button, and I'll click OK, jump over to the emulator. Here is my app. If you are presented with a permission dialog asking for location permission, then go ahead and just click, allow.
What I'm going to do is bring up the location tools and I'm going to enter the coordinates for Union Square in San Francisco. What I'm gonna do first is bring up the maps app because again I wanna make sure that my emulator has a valid current place. What I'll do is I'll enter 37.7880 and minus 122.4074 and click send, and sure enough that's Union Square in San Francisco.
I'll close this and we'll jump back to my app. Back here in the app I'm going to click the trigger the Place Picker button, and I'm gonna click on the 'not now' for the improve location accuracy. Users might see this sometimes if they don't have their WiFi enabled and this is basically offering to improve the location accuracy using WiFi and cell networks. I'll just say, not now. You can see that when the Place Picker comes up there is a nice UI that I can use to choose a place from.
There is a map which I can use to scroll around the neighborhood and there is a list. I can scroll through the list or I can pick a location directly from the map. Let's go ahead and choose Union Square, and you can see that when I choose the location the Place Picker goes away and the information is updated back in my UI. Let's go ahead and try something a little more advanced now. I'm gonna stop this app and what I'm going to do is specify a set of bounding coordinates.
Let's go back up to our pickAPlace function, and what I'm going to do is set what's called a view pot. A view pot is going to be a set of bounding coordinates that I'm going to pin the Place Picker to. First let's create a new LAT long bounds object. What I'm gonna write here is LATLngBounds equals new LATLngBoundsBuilder. This is one of the things you learned about working with many of the Google APIs, is that almost everything has some kind of a builder associated with it.
I'm going to write, include a new LATLng point, which I'm going to set to 47.608670, minus 122.340033 and I'm gonna include another point 47.610005 and minus 122.342865, and then dot build.
This set of bound specifies the area around the Pike Place Market in Seattle. In order to use this new bounding area, we have to include this in the intent builder. Right after the builder call here, I'm going to intent that right dot, set LATLngBounds to bounds and then put the period in front of the build call. Now what I've done is I have constrained the Place Picker to an area right around the Pike Place Market in Seattle.
Let's save. Now let's run the app again, and once again I'll use my emulator. Here we are back in the app. Now I'm gonna trigger the picker again. I will just dismiss my dialog again, and you can see that this is the area around the Pike Place Market in Seattle, here is all the locations that are currently within that location. What I'm going to do, I can just go ahead and pick the Pike Place Market, and you see the picker goes away and there is the information about the market right here in my UI.
By using a Place Picker you can display a nice flexible UI that gives users a really easy way to select places within your app.
Programming expert Joe Marini covers how to retrieve and work with a user's location and get continuous location updates with Location services; target and authenticate users and access their profile information with Google Sign-In; and work with the Google Drive API to store and retrieve content. Joe also shows you how to use the Google Places API to get information about places such as addresses, phone numbers, and website URLs, along with how to send messages to individual devices using Google Cloud Messaging. By the end of this course, you should be able to incorporate Google Play services into your own apps and take advantage of the various features it provides.
- Using Google Play services in your Android project
- How the Google Play services life cycle works
- Retrieving the user's location with Location services
- Working with location data
- Performing a geocoding operation
- Using Google Sign-In to access user information
- Storing information with the Google Drive API
- Working with the Google Places API
- Communicating via Google Cloud Messaging (GCM)