How to Draw Line on Google Maps in Android
In this article, we will discuss how to draw route between two locations in GoogleMap using Directions API .
Directions API
The Directions API is a service that calculates directions between locations using an HTTP request.
With the Directions API, you can:
- Search for directions for several modes of transportation, including transit, driving, walking or cycling.
- Return multi-part directions using a series of waypoints.
- Specify origins, destinations, and waypoints as text strings (e.g. "Chicago, IL" or "Darwin, NT, Australia"), or as latitude/longitude coordinates, or as place IDs.
GET GITHUB CODE FROM HERE.
Creating New Project
1. Create a new project in Android Studio from File ⇒ New Project and fill the project details.
2. In the Choose your projectdialog, select the tab that corresponds to the platform you intended to develop for. Select Phone and Tablet .
3. Select Google Maps Activity , then click Next .
4.Enter your app name, package name, and project location, programming language (Java or Kotlin), and the minimum Android API level supported by your app, then click Finish .
Getting the API key
5.Copy the URL from google_map_api.xml file to generate Google map API key.
6.Paste the copied URL at the browser. It will open the following page. Here I have selected my project GoogleMapExample.
7.Click on Create API key to generate API key.
8.After clicking on Create API key , it will generate an API key displaying the following screen.
9.Copy this generated API key and click on the CLOSE button. Now paste it in your google_map_api.xml file.
10.Select Library from the left side and then search for Direction API and enable it.
11.Go to Credentials. Click on the currently created API key and the following screen appears:
12 .Under Application restrictions select None and under API restrictions select Restrict Key and then select the two APIs Direction API and Maps SDK for Android.Finally, don't forget to click on SAVE .
13.After creating an API key you must enable Billing on the Google Cloud Project at https://console.cloud.google.com/project/_/billing/enable . Learn more at https://developers.google.com/maps/gmp-get-started .
14.Open build.gradle.Here you can see a new library to integrate google Maps gets added.
build.gradle
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' //Google Maps implementation 'com.google.android.gms:play-services-maps:16.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' }
15.Open AndroidManifest.xml file and add the below user-permissions as shown below.
INTERNET – To communicate with API .
ACCESS_COARSE_LOCATION– To determine user's location using WiFi and mobile cell data. It will give you an approximate location.
ACCESS_FINE_LOCATION – To determine user's location using GPS. It will give you precise location.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mapdirectiondemo"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MapsActivity" android:label="@string/title_activity_maps"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" /> </application> </manifest>
16.Google maps are implemented using Support MapFragment which is a subclass of Fragment class. By default, the XML file that defines the app's layout is at res/layout/activity_maps.xml . It contains the following code:
activity_maps.xml
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MapsActivity" />
17.Let's understand the methods and AsyncTask used in MainActivity.Java .
getDirectionsUrl(): Getting URL to the Google Directions API based on output, parameters, and the API key.
private String getDirectionsUrl(LatLng origin, LatLng dest) { // Origin of route String str_origin = "origin=" + origin.latitude + "," + origin.longitude; // Destination of route String str_dest = "destination=" + dest.latitude + "," + dest.longitude; // Setting mode String mode = "mode=driving"; // Building the parameters to the web service String parameters = str_origin + "&" + str_dest + "&" + mode; // Output format String output = "json"; // Building the url to the web service String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters + "&key=" + "AIzaSyD_L8g3AcwXBKnEjhvLJwBXwI3L51LjQUU"; return url; }
A Directions API request takes the following form:
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters + "&key=" + "API KEY";
The output may be either of the following values json or xml . In my case, it holds a "json" string and the parameter string is created as:
String parameters = str_origin + "&" + str_dest + "&" + mode;
str_origin and str_dest : The address, textual latitude/longitude value, or place ID from which you wish to calculate directions.
We have set the mode=driving in the current application.
The other modes of transport are:
- driving (default)
- walking
- bicycling
- transit
downloadUrl(): A method to download JSON data using HttpURLConnection from the URL returned by getDirectionsUrl() .
private String downloadUrl(String strUrl) throws IOException { String data = ""; InputStream iStream = null; HttpURLConnection urlConnection = null; try { URL url = new URL(strUrl); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); iStream = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } data = sb.toString(); br.close(); } catch (Exception e) { Log.d("Exception", e.toString()); } finally { iStream.close(); urlConnection.disconnect(); } return data; }
ParserTask : ParserTask is a class that extends AsyncTask . Here we will parse the JSON data returned by downloadUrlmethod.
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { // Parsing the data in non-ui thread @Override protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { JSONObject jObject; List<List<HashMap<String, String>>> routes = null; try { jObject = new JSONObject(jsonData[0]); DataParser parser = new DataParser(); routes = parser.parse(jObject); } catch (Exception e) { e.printStackTrace(); } return routes; } @Override protected void onPostExecute(List<List<HashMap<String, String>>> result) { ArrayList points = new ArrayList(); PolylineOptions lineOptions = new PolylineOptions(); for (int i = 0; i < result.size(); i++) { List<HashMap<String, String>> path = result.get(i); for (int j = 0; j < path.size(); j++) { HashMap<String, String> point = path.get(j); double lat = Double.parseDouble(point.get("lat")); double lng = Double.parseDouble(point.get("lng")); LatLng position = new LatLng(lat, lng); points.add(position); } lineOptions.addAll(points); lineOptions.width(12); lineOptions.color(Color.RED); lineOptions.geodesic(true); } // Drawing polyline in the Google Map if (points.size() != 0) mMap.addPolyline(lineOptions); } }
In the doInBackround method, we will parse the JSON data. So we are parsing the data using another class i.e DataParser.
In the onPostExecutemethod, we will add polyline to draw a route on Google Map.
DataParser:Right-click on root project directory under Java folder and create a new Java Class DataParser .Java and write the below code:
DataParser.Java
package com.example.mapdirectiondemo; import com.google.android.gms.maps.model.LatLng; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class DataParser { /** * Receives a JSONObject and returns a list of lists containing latitude and longitude */ public List<List<HashMap<String, String>>> parse(JSONObject jObject) { List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String, String>>>(); JSONArray jRoutes = null; JSONArray jLegs = null; JSONArray jSteps = null; try { jRoutes = jObject.getJSONArray("routes"); /** Traversing all routes */ for (int i = 0; i < jRoutes.length(); i++) { jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs"); List path = new ArrayList<HashMap<String, String>>(); /** Traversing all legs */ for (int j = 0; j < jLegs.length(); j++) { jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps"); /** Traversing all steps */ for (int k = 0; k < jSteps.length(); k++) { String polyline = ""; polyline = (String) ((JSONObject) ((JSONObject) jSteps.get(k)).get("polyline")).get("points"); List list = decodePoly(polyline); /** Traversing all points */ for (int l = 0; l < list.size(); l++) { HashMap<String, String> hm = new HashMap<String, String>(); hm.put("lat", Double.toString(((LatLng) list.get(l)).latitude)); hm.put("lng", Double.toString(((LatLng) list.get(l)).longitude)); path.add(hm); } } routes.add(path); } } } catch (JSONException e) { e.printStackTrace(); } catch (Exception e) { } return routes; } /** * Method to decode polyline points */ private List decodePoly(String encoded) { List poly = new ArrayList(); int index = 0, len = encoded.length(); int lat = 0, lng = 0; while (index < len) { int b, shift = 0, result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += dlng; LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5))); poly.add(p); } return poly; } }
parse() method will receive a JSONObject and returns a list of lists containing latitude and longitude.
Here we will split JSONArray by using getJSONArray
And we will traverse all routes, legs, steps, points, etc. And then we will add all the traversing points to the list.
routes.add(path);
decodepoly() : Method to decode polyline points.
Complete Code:
18.Open MainActivity.Java and write the below code:
MainActivity.Java
package com.example.mapdirectiondemo; import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.PolylineOptions; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import androidx.fragment.app.FragmentActivity; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; MarkerOptions origin, destination; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); //Setting marker to draw route between these two points origin = new MarkerOptions().position(new LatLng(12.9121, 77.6446)).title("HSR Layout").snippet("origin"); destination = new MarkerOptions().position(new LatLng(12.9304, 77.6784)).title("Bellandur").snippet("destination"); // Getting URL to the Google Directions API String url = getDirectionsUrl(origin.getPosition(), destination.getPosition()); DownloadTask downloadTask = new DownloadTask(); // Start downloading json data from Google Directions API downloadTask.execute(url); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.addMarker(origin); mMap.addMarker(destination); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(origin.getPosition(), 10)); } private class DownloadTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... url) { String data = ""; try { data = downloadUrl(url[0]); } catch (Exception e) { Log.d("Background Task", e.toString()); } return data; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); ParserTask parserTask = new ParserTask(); parserTask.execute(result); } } /** * A class to parse the JSON format */ private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { // Parsing the data in non-ui thread @Override protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { JSONObject jObject; List<List<HashMap<String, String>>> routes = null; try { jObject = new JSONObject(jsonData[0]); DirectionsJSONParser parser = new DirectionsJSONParser(); routes = parser.parse(jObject); } catch (Exception e) { e.printStackTrace(); } return routes; } @Override protected void onPostExecute(List<List<HashMap<String, String>>> result) { ArrayList points = new ArrayList(); PolylineOptions lineOptions = new PolylineOptions(); for (int i = 0; i < result.size(); i++) { List<HashMap<String, String>> path = result.get(i); for (int j = 0; j < path.size(); j++) { HashMap<String, String> point = path.get(j); double lat = Double.parseDouble(point.get("lat")); double lng = Double.parseDouble(point.get("lng")); LatLng position = new LatLng(lat, lng); points.add(position); } lineOptions.addAll(points); lineOptions.width(12); lineOptions.color(Color.RED); lineOptions.geodesic(true); } // Drawing polyline in the Google Map if (points.size() != 0) mMap.addPolyline(lineOptions); } } private String getDirectionsUrl(LatLng origin, LatLng dest) { // Origin of route String str_origin = "origin=" + origin.latitude + "," + origin.longitude; // Destination of route String str_dest = "destination=" + dest.latitude + "," + dest.longitude; //setting transportation mode String mode = "mode=driving"; // Building the parameters to the web service String parameters = str_origin + "&" + str_dest + "&" + sensor + "&" + mode; // Output format String output = "json"; // Building the url to the web service String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters + "&key=" + "AIzaSyD_L8g3AcwXBKnEjhvLJwBXwI3L51LjQUU"; return url; } /** * A method to download json data from url */ private String downloadUrl(String strUrl) throws IOException { String data = ""; InputStream iStream = null; HttpURLConnection urlConnection = null; try { URL url = new URL(strUrl); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); iStream = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } data = sb.toString(); br.close(); } catch (Exception e) { Log.d("Exception", e.toString()); } finally { iStream.close(); urlConnection.disconnect(); } return data; } }
When you run your application it will look like this:
I hope this article will help you in understanding how to draw a route between two locations using Directions API. Thank You.
How to Draw Line on Google Maps in Android
Source: https://c1ctech.com/android-googlemap-example-to-draw-route-between-two-locations/
0 Response to "How to Draw Line on Google Maps in Android"
Publicar un comentario