Basic Google Maps API Android Tutorial + Google Maps Directions API

Basic Google Maps API Android Tutorial + Google Maps Directions API - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Basic Google Maps API Android Tutorial + Google Maps Directions API, we have prepared this article for you to read and download the information therein. hopefully fill posts article +, article android, article API, article Basic, article Directions, article Google, article Maps, article Tutorial, we can write this you understand. Well, happy reading.

Title : Basic Google Maps API Android Tutorial + Google Maps Directions API
link : Basic Google Maps API Android Tutorial + Google Maps Directions API

Baca juga


Basic Google Maps API Android Tutorial + Google Maps Directions API

Hello guys, this is Mai Thanh Hiep.

Today, I gonna show you step by step how to use basic Google Map API on Android Programming After completely watching this video, you can create your own an application such as finding a path from 2 any places on map like below demo application The first time, this will show place of University of Science, “Dai hoc khoa hoc tu nhien” You can also show other places, it's up to you You have to fill out the original address and destination For an example, enter original address “Dai hoc khoa hoc tu nhien”, destination “Ben thanh” and click Find Path button Wow, App will show a path from “Trung tam tin hoc Dai hoc khoa hoc tu nhien” to “Ben Thanh, district 1, Ho Chi Minh, Viet Nam” and some others information such as the distance between two places is 2.

3 km and appoximate time moving is 10 minutes.

Wow, it sounds interesting, aren't you? Now, we will together dive into Google Map API First of all, we create a new project in android studio name your project for an example GoogleMapAPIDemo Next Next Choose Google Maps Activity Next Finish After creating new project with Google Maps Activity, android studio will show the screen of google_map_api.

Xml and you need Google Maps API key for using this google map service.

To register a key, you go to Google API Console If your account hasn’t have any projects yet, please create the new one.

Click “create a project” button, fill your project name in this field and click “Create” button.

After done at creating a project, you need to go to Google Maps Android API to enable this API for this project -> Click “enable” button After enabling, you can go to Credentials to create a key for your application Click “Create credentials” button -> API key -> Server key -> name the key -> Create -> Ok Copy this key and paste to “YOUR_KEY_HERE” place, that’s it, so easy! Ok, we will see overview the source code that android studio generated We have a class named SupportMapFragment and this code line will retrieve the map view in UI that you need to interact with programmatically.

GetMapAsync(this) function will start map service.

After succesfully starting map service, this onMapReady will be called This code line will create a sydney position with latitude is -34 and longitude is 151.

AddMarker will add a marker at sydney position to the map with title is “Marker is Sydney”.

MoveCamera will move the center of screen to sydney position Now, we will run and see the result Ok, double click to zoom it, ok that’s sydney position Now I will change it to my university “Dai hoc khoa hoc tu nhien” Go to “maps.

Google.

Com” to get this place coordinate Right click this place and click “What’s here?” To zoom sydney in level 18, use.

NewLatLngZoom(sydney, 18) function Run it This is “Khoa hoc tu nhien” with a marker title “Khoa hoc tu nhien TP HCM” Ok, to set different types in Google Map, we use setMapType function Google supports 3 types of map: MAP_TYPE_NORMAL, MAP_TYPE_SATELLITE, MAP_TYPE_TERRAIN Now, I will change to MAP_TYPE_SATELLITE and run it Ok, this is satellite map now To allow user show their current location, we use setMyLocationEnabled(true), alt+enter -> Run Click my location icon and see the result Screen moved to my current location with a blue point aimed my location To add a marker with our custom icon, add a function.

Icon(…) to MarkerOptions() like this Umh, copy an icon to drawable folder and we use it as a custom icon Ok, run it To draw polylines to map through given coordinates, use.

AddPolyline(…) like this Add a list of LatLng coordinates to add(…) function, that this polyline will go through Create another LatLng coordinate named dhsg Add more 2 mid points Set width is 10, and color is RED Ok, run it Ok, now I will teach you how to find a path between 2 address places using Google Maps Direction API You can search this API from google The output maybe json or xml, google recommend you to use json format instead of xml And some parameters, but the most important ones are origin, destination and key origin can be LatLng coordinate or address text or place_id destination is end place that the same format to origin key is your Google API Key that you created before But you need to enable Google Maps Direction API of your project from Google Console Choose your project Search Direction API and choose Google Maps Direction API and enable it Ok, just copy your project key For an example, I query data from “Dai hoc khoa hoc tu nhien” to “Ben Thanh” and get the json result There are so many results, but you can focus on routes result, this is an array json objects end_address: “Ben Thanh, Quan 1, Ho Chi Minh, Viet Nam” this is the end place that google detected from your input “Ben Thanh” end_location is a pair latitude and longitude of the end place start_address: “Trung tam tin hoc truong ….

” This is the start place that google detected from your input “Dai hoc khoa hoc tu nhien” This one mean the distance is “2.

3 km” and aproximate is “10 mins” from origin place to the destination You can read on Google Maps Direction API document for more details overview_polyline: this is coordinates of the route from origin to destination that is encoded, you need to decode it Ok, now I will teach you how to create simple android application that find path between 2 places by explaining my code and I will share this code to github, and you can find the download link from this youtube description.

We need 2 Edit Text controls, the first one for origin place and the second for the destination A button find path to do the action And a google map view when user click “Find Path” button, it will invoke sendRequest function Get text of origin, destination from 2 EditText controls and check if they are empty If the validation is ok, it will delegate to DirectionFinder object to executing finding a path In execute function, it will download raw json data from link first We need a CreateUrl() function to create a link from origin, destination and key from user input.

URLEncode will encode user input to URL format After having got the link, we need an independence thread from UI thread to download raw data, because it takes a long time to download raw data new Url(link).

OpenConnection().

GetInputStream and get the result string data After completely downloading data, onPostExecute will be processed and parseJson is invoked to get necessary information JSONObject jsonData = new JSONObject(data) will create a JSONObject from a data string.

and we need routes information it is JSONArray because it starts with ‘[‘ character so we call jsonData.

GetJSONArray(“routes”) to get routes JSONArray from jsonData Generally, this route is only one entry, but it maybe more than one if you pass alternatives=true Foreach array objects, get each json object and process it like this We need decodePolyLine function to decode overview_polylines of a current route and it returns a list of LatLng coordinates of the route from the origin and destination You can code this function by your self or search it from google After completely json processing, it will return List<Route> routes, and listener invoke interface function “onDirectionFinderSuccess” to display the result to map Because Main Activity is implemented this interface move screen and zoom at startLocation Route is generally only one entry Zoom at startLocation in level 16 We will set text of distance and duration to show it And we will create a marker at startLocation and a marker at endLocation with its corresponding title We will create a polylineOptions and then add list of LatLng coordinates to it After that, we will add polylineOptions to mMap we also need polylinePath to store polylines that mMap.

AddPolyline return, for us to remove it later in case we don’t need it anymore remove them if we don't need these polylines anymore Or else these polyline nerver disappear in map view Ok, see demo again Done! Now you can create your own application like my demo app using Google Maps API Addition, you can search other common Google API such as “Place Autocomplete” This will predict user input type and auto complete it Or Google Maps Geocoding API to convert address text to a LatLng coordinate It will return LatLng coordinate from an address text and other useful information Ok, thank you guys for watching this video please comment if you have any problems and click subscribe below to get new video tutorials from me!.



Thus Article Basic Google Maps API Android Tutorial + Google Maps Directions API

That article Basic Google Maps API Android Tutorial + Google Maps Directions API This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Basic Google Maps API Android Tutorial + Google Maps Directions API the link address http://svedda79.blogspot.com/2016/06/basic-google-maps-api-android-tutorial.html

0 Response to "Basic Google Maps API Android Tutorial + Google Maps Directions API"

Post a Comment