{"id":48763,"date":"2017-07-06T11:17:56","date_gmt":"2017-07-06T05:47:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=48763"},"modified":"2017-07-06T11:17:56","modified_gmt":"2017-07-06T05:47:56","slug":"integration-of-google-map-apis","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integration-of-google-map-apis\/","title":{"rendered":"Integration of Google Map APIs"},"content":{"rendered":"<p style=\"text-align: justify\">Software engineers and organisations lookout for effective ways to create new applications that save time and money. We can easily do this by using third-party APIs and SDKs.<\/p>\n<p style=\"text-align: justify\">This blog talks about a useful Google Map API. I am working on a project\u00a0based on Routing and live tracking of a driver&#8217;s vehicle.<\/p>\n<p style=\"text-align: justify\">Consider a scenario where a driver can pick up multiple passengers from different locations, where we need to put markers on every pickup. A route needs to be created between two locations. Google Maps API allows you to display maps, draw routes and create Markers on your website.<\/p>\n<p style=\"text-align: justify\">Following are the steps to\u00a0use Google Map APIs:<\/p>\n<p style=\"text-align: justify\"><strong>Step1:<\/strong> \u00a0Add script to your HTML page.<\/p>\n<pre>&lt;script src=\"https:\/\/maps.googleapis.com\/maps\/api\/js?key=[YOUR_API_KEY]&amp;libraries=places\"&gt;&lt;\/script<\/pre>\n<p style=\"text-align: justify\"><strong>Step2:<\/strong>\u00a0Get your API key. Click the Get A Key button from <a href=\"https:\/\/developers.google.com\/maps\/documentation\/directions\/get-api-key\">here<\/a>. It will tell you how to register projects in the Google API Console. It can also activate the\u00a0Directions API automatically to generate a generic and unrestricted API key.<\/p>\n<p style=\"text-align: justify\"><strong>Step3:\u00a0<\/strong>Now we can use multiple Google API&#8217;s in our controller to fulfil our requirements:<\/p>\n<ol style=\"text-align: justify\">\n<li>Draw a route between two locations either by location name or location lat-long:\n<p>[js]<br \/>\nvar directionsDisplay;<br \/>\nvar directionsService = new google.maps.DirectionsService();<br \/>\nvar map;<br \/>\nvar mapCenter = new google.maps.LatLng(28.535891, 77.345700);<br \/>\nvar myOptions =<br \/>\n    {<br \/>\n        zoom: 12,<br \/>\n        mapTypeId: google.maps.MapTypeId.ROADMAP,<br \/>\n        center: mapCenter<br \/>\n    };<br \/>\nmap = new google.maps.Map(document.getElementById(&quot;map_canvas&quot;), myOptions);<br \/>\n\/\/There should be a div with id &#8216;map_canvas&#8217; in your html.<br \/>\n\/\/It can be done in angular directive if you are using angular<\/p>\n<p>var request = {.<br \/>\n   origin: &#8216;Sector 127, Noida&#8217;,<br \/>\n   destination: &#8216;Sector 18, Noida&#8217;,<br \/>\n   travelMode: &#8216;DRIVING&#8217;<br \/>\n} <\/p>\n<p>       OR<\/p>\n<p>var request = {<br \/>\n   origin: new google.maps.LatLng(28.535891, 77.345700),<br \/>\n   destination: new google.maps.LatLng(28.570317, 77.321820),<br \/>\n   travelMode: &#8216;DRIVING&#8217;<br \/>\n}<\/p>\n<p>directionsService.route(request, function(response, status) {<br \/>\n      if (status === &#8216;OK&#8217;) {<br \/>\n          directionsDisplay = new google.maps.DirectionsRenderer();<br \/>\n          directionsDisplay.setMap(map);<br \/>\n          directionsDisplay.setDirections(response);<br \/>\n          \/\/ For each route, display summary information.<br \/>\n      } else {<br \/>\n          console.log(&#8216;Directions request failed due to &#8216; + status, response);<br \/>\n      }<br \/>\n});<\/p>\n<p>[\/js]<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-48809\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/05\/Route11-1024x575.png\" alt=\"Route1\" width=\"625\" height=\"350\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/05\/Route11-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/05\/Route11-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/05\/Route11-624x350.png 624w, \/blog\/wp-ttn-blog\/uploads\/2017\/05\/Route11.png 1366w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>In the above image, the polyline is the default polyline\u00a0of directionService to draw the route.<\/li>\n<li>We can also change polyline configurations like colour and line style by specifying polylineOptions while creating the directionsDisplay object. See the code below:\n<p>[js]<br \/>\n\/\/ we can specify even small dot-dot and any other kind of polyline according to requirement<br \/>\nvar lineSymbol = {<br \/>\n    path: &#8216;M 0,-1 0,1&#8217;,<br \/>\n    strokeOpacity: 1,<br \/>\n    scale: 4<br \/>\n};<\/p>\n<p>var polylineOptionsActual = {<br \/>\n    strokeColor: &#8216;#FB802E&#8217;,<br \/>\n    strokeOpacity:0,<br \/>\n    strokeWeight: 0,<br \/>\n    icons: [{<br \/>\n        icon: lineSymbol,<br \/>\n        offset: &#8216;0&#8217;,<br \/>\n        repeat: &#8217;20px&#8217;<br \/>\n    }]<br \/>\n};<br \/>\n\/\/specify polylineOption in DirectionRenderer constructor<br \/>\ndirectionsDisplay = new google.maps.DirectionsRenderer({polylineOptions: polylineOptionsActual});<\/p>\n<p>[\/js]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-48817\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/05\/dotted_route-1024x575.png\" alt=\"dotted_route\" width=\"625\" height=\"350\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/05\/dotted_route-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/05\/dotted_route-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/05\/dotted_route-624x350.png 624w, \/blog\/wp-ttn-blog\/uploads\/2017\/05\/dotted_route.png 1366w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>In this image, Google detects the smallest route between location A and B by default. However, if we want to find a route covering some specific locations like that in the &#8220;<strong>Travelling Salesman Problem&#8221;\u00a0<\/strong>then, we can define <strong>waypoints array<\/strong> for those places while drawing the route.<\/li>\n<li>Creating Waypoints while Drawing Route:\n<p>[js]<br \/>\nfunction drawRouteByWaypoints() {<br \/>\n            \/\/covering Location Array<\/p>\n<p>      var otherLocations = [<br \/>\n      \t\t{latitude: 28.535618, longitude: 77.348914},<br \/>\n      \t\t{latitude: 28.555861, longitude: 77.329794},<br \/>\n      \t\t{latitude: 28.561299, longitude: 77.336473}<br \/>\n      ];<br \/>\n      var wayPoints = [];<br \/>\n      angular.forEach(otherLocations, function (waypoint) {<br \/>\n          wayPoints.push({<br \/>\n              location: new google.maps.LatLng(waypoint.latitude, waypoint.longitude),<br \/>\n              stopover: true<br \/>\n      \t  });<br \/>\n      });<br \/>\n      var request = {<br \/>\n          origin: &#8216;Sector 127, Noida&#8217;,<br \/>\n          destination: &#8216;Sector 18, Noida&#8217;,<br \/>\n          waypoints: wayPoints,<br \/>\n          optimizeWaypoints: true,<br \/>\n          travelMode: &#8216;DRIVING&#8217;<br \/>\n      };<br \/>\n      directionsService.route(request, function(response, status) {<br \/>\n          if (status === &#8216;OK&#8217;) {<br \/>\n              directionsDisplay = new google.maps.DirectionsRenderer();<br \/>\n              directionsDisplay.setMap(map);<br \/>\n              directionsDisplay.setDirections(response);<br \/>\n              \/\/ For each route, display summary information.<br \/>\n          } else {<br \/>\n          \t  console.log(&#8216;Directions request failed due to &#8216; + status, response);<br \/>\n          }<br \/>\n      });<br \/>\n}<br \/>\n[\/js]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-48908\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/05\/Waypoint_route-1024x575.png\" alt=\"Waypoint_route\" width=\"625\" height=\"350\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/05\/Waypoint_route-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/05\/Waypoint_route-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/05\/Waypoint_route-624x350.png 624w, \/blog\/wp-ttn-blog\/uploads\/2017\/05\/Waypoint_route.png 1366w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>In the image above, B, C and D are the locations that we need to cover between Location A (Sector 127, Noida) and Location B (Sector 18, Noida).<\/p>\n<p>We can also avoid displaying B, C, and D markers for waypoints by passing <strong>{suppressMarkers: true}<\/strong> in the DirectionRenderer constructor while creating a directionsDisplay object as given below:<\/p>\n<p>[js]<br \/>\n directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});<br \/>\n [\/js]<\/p>\n<\/li>\n<li>Creating Markers on the map: Here we will discuss different types of markers that we can create by using Google APIs:<br \/>\n<strong>a. Default Google Marker<\/strong><\/p>\n<p>[js]<\/p>\n<p>    var marker = new google.maps.Marker({<br \/>\n        map: map,<br \/>\n        position: {lat: 28.533938, lng: 77.348235}<br \/>\n    });<\/p>\n<p>    [\/js]<\/p>\n<p><strong>\u00a0b. Marker with Label<\/strong><\/p>\n<p>[js]<\/p>\n<p>    var marker = new google.maps.Marker({<br \/>\n        map: map,<br \/>\n        label: &#8216;1&#8217;,<br \/>\n        position: {lat: 28.543792, lng: 77.331007}<br \/>\n    });<\/p>\n<p>    [\/js]<\/p>\n<p><strong>c.<\/strong> <strong>Marker with Custom colour<\/strong><\/p>\n<p>[js]<\/p>\n<p>    var marker = new google.maps.Marker({<br \/>\n          map: map,<br \/>\n          position: {lat: 28.570317, lng: 77.321820},<br \/>\n          icon: pinSymbol(&quot;#FFF&quot;)<br \/>\n    });<\/p>\n<p>    function pinSymbol(color) {<br \/>\n        return {<br \/>\n            path: &#8216;M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z&#8217;,<br \/>\n            fillColor: color,<br \/>\n            fillOpacity: 1,<br \/>\n            strokeColor: &#8216;#000&#8217;,<br \/>\n            strokeWeight: 2,<br \/>\n            scale: 1,<br \/>\n        };<br \/>\n    }<\/p>\n<p>    [\/js]<\/p>\n<p><strong>d. Marker with Custom Image<\/strong><\/p>\n<p>[js]<\/p>\n<p>    var marker = new google.maps.Marker({<br \/>\n        map: map,<br \/>\n        position: {lat: 28.573405, lng: 77.371203},<br \/>\n        icon: &#8216;https:\/\/developers.google.com\/maps\/documentation\/javascript\/examples\/full\/images\/beachflag.png&#8217;<br \/>\n    });<\/p>\n<p>    [\/js]<\/p>\n<\/li>\n<\/ol>\n<p style=\"text-align: justify\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-49058\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/Markers1-1024x575.png\" alt=\"Markers1\" width=\"625\" height=\"350\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/Markers1-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/Markers1-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/Markers1-624x350.png 624w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/Markers1.png 1366w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p style=\"text-align: justify\">As we can see, all the markers are those which we created by using Google API. Nevertheless, we can create markers with a different look and feel by using some external JS.<\/p>\n<p style=\"text-align: justify\">1. <strong>markerwithlabel.js:<\/strong> We can create marker of any colour with any label.<\/p>\n<p>[js]<\/p>\n<p>var marker = new MarkerWithLabel({<br \/>\n      position: {lat: 28.529377, lng: 77.391295},<br \/>\n      map: map,<br \/>\n      labelContent: 1,<br \/>\n      labelAnchor: new google.maps.Point(7, 35),<br \/>\n      labelClass: &quot;labels&quot;, \/\/ the CSS class for the label<br \/>\n      labelInBackground: false,<br \/>\n      icon: pinSymbol(&#8216;red&#8217;)\/\/it is function that is given in above code<br \/>\n});<\/p>\n<p>\/\/the CSS class for label<br \/>\n.labels {<br \/>\n      color: white;<br \/>\n      background-color: transparent;<br \/>\n      font-size: 12px;<br \/>\n      text-align: center;<br \/>\n      width: 15px;<br \/>\n      white-space: nowrap;<br \/>\n      font-weight: 600;<br \/>\n      top: 121px;<br \/>\n}<br \/>\n[\/js]<\/p>\n<p style=\"text-align: justify\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-49210\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithLabel1-1024x575.png\" alt=\"MarkerWithLabel\" width=\"625\" height=\"350\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithLabel1-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithLabel1-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithLabel1-624x350.png 624w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithLabel1.png 1366w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p style=\"text-align: justify\">Click\u00a0<a href=\"https:\/\/github.com\/nmccready\/google-maps-utility-library-v3-markerwithlabel\/blob\/master\/docs\/examples.html\">here<\/a> to explore more about it.<\/p>\n<p style=\"text-align: justify\">2.<strong> map-icons.js:<\/strong> By using Map Icons, we can change\u00a0<strong>Google Maps Marker<\/strong>\u00a0according to our requirements and we can have control over the shape,\u00a0color, and size of Marker. We can\u00a0also extend <strong>Google Map Marker<\/strong> object by placing an icon on top of Marker as a label.<\/p>\n<p style=\"text-align: justify\">Include the following things from <a href=\"http:\/\/map-icons.com\/\">here<\/a> to create markers with icons:<br \/>\na. fonts given in dist\/font<br \/>\nb. dist\/css\/map-icons.css<br \/>\nc. dist\/js\/map-icons.js<\/p>\n<p style=\"text-align: justify\">Icon Classes: icon Class names are to be used with the <strong>map-icon<\/strong> class prefix.<\/p>\n<p>[js]<br \/>\n&lt;span class=&quot;map-icon map-icon-point-of-interest&quot;&gt;&lt;\/span&gt;<\/p>\n<p>[\/js]<\/p>\n<p style=\"text-align: justify\">You should be creating an instance of <strong>Marker<\/strong> rather than <strong>google.maps.Marker<\/strong>.<\/p>\n<p>[js]<\/p>\n<p>var marker = new Marker({<br \/>\n    position: {lat: 28.533938, lng: 77.348235},<br \/>\n    map: map,<br \/>\n    icon: {<br \/>\n        path: &#8216;M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z&#8217;,<br \/>\n        fillColor: &#8216;#0000FF&#8217;,<br \/>\n        fillOpacity: 1,<br \/>\n        strokeColor: &#8221;,<br \/>\n        strokeWeight: 0<br \/>\n    },<br \/>\n    map_icon_label: &quot;&lt;span class=&#8217;map-icon map-icon-bus-station&#8217;&gt;&lt;\/span&gt;&quot;<br \/>\n});<br \/>\n[\/js]<\/p>\n<p style=\"text-align: justify\">Explicit styles to icons used on a Google marker should be applied with .map-icon-label .map-icon CSS selector.<\/p>\n<p>[js]<br \/>\n.map-icon-label .map-icon {<br \/>\n    display: block;<br \/>\n    font-size: 12px;<br \/>\n    color: white;<br \/>\n    width: 49px;<br \/>\n    line-height: 39px;<br \/>\n    text-align: center;<br \/>\n    white-space: nowrap;<br \/>\n}<br \/>\n[\/js]<\/p>\n<p style=\"text-align: justify\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-49235\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithMapIcon-1024x575.png\" alt=\"MarkerWithMapIcon\" width=\"625\" height=\"350\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithMapIcon-1024x575.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithMapIcon-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithMapIcon-624x350.png 624w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/MarkerWithMapIcon.png 1366w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p style=\"text-align: justify\">To explore Google Map API, <a href=\"https:\/\/developers.google.com\/maps\/documentation\/javascript\/tutorial\">please visit here<\/a>.<\/p>\n<p style=\"text-align: justify\">You can also visit code on <a href=\"https:\/\/github.com\/mahimaag\/google-map-api\">GitHub repo<\/a><\/p>\n<p style=\"text-align: justify\">In this way, you can easily create routes and markers on Map with just a few steps. Hope this blog was helpful to you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Software engineers and organisations lookout for effective ways to create new applications that save time and money. We can easily do this by using third-party APIs and SDKs. This blog talks about a useful Google Map API. I am working on a project\u00a0based on Routing and live tracking of a driver&#8217;s vehicle. Consider a scenario [&hellip;]<\/p>\n","protected":false},"author":810,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[1439,1994,1],"tags":[4849,323,245,398,4611,55,3626,4620,1418,1610,4642],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/48763"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/810"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=48763"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/48763\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=48763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=48763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=48763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}