Giter Club home page Giter Club logo

drawroutes's Introduction

Draw Google Map Routes from Two Points.

  • Go to Google Console -> Enable APIs and Services -> and enable google Directions API and Maps SDK for Android
  • back to Google Console -> Create Credentials -> API KEY and copy the api key.

Android Studio

  • Create new Google Maps Activity

Manifest

1- add internet permission (essential) and fine location (optional)

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

2- add meta tags

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" /> 

app gradle

1- Add secrets_gradle_plugin in the plugins to be..

plugins {
    id 'com.android.application'
    id 'com.google.secrets_gradle_plugin' version '0.5'
}

2-Don't forget to add play-services-maps dependency.

implementation 'com.google.android.gms:play-services-maps:17.0.0'

In Values/google_maps_api.xml (add your api key)

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">API_KEY</string>

Map Activity..

1-Copy util to your package util

2-Add this code in onMapReady() callback..

       //set the source and the dest path
        LatLng source = new LatLng(53.474353213819775, -2.194336680184662);
        LatLng destination = new LatLng(53.62132199731562, -2.153090419727381);
        //read the API_Key from google_maps_key
        String API_KEY = getResources().getString(R.string.google_maps_key);
        //return a polyLine for the route from source to the dest.
        new GetPathFromLocation(source, destination, API_KEY, new DirectionPointListener() {
            @Override
            public void onPath(PolylineOptions polyLine) {
                //when the path is retrieved, plot it on the map
                map.addPolyline(polyLine);
            }
        }).execute();

        //add markers to the src,dst locations.
        map.addMarker(new MarkerOptions().position(source).title("SOURCE"));
        map.addMarker(new MarkerOptions().position(destination).title("DEST"));
        //zoom to the source location with scale 12..
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(source, 12));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.