Introduction

This article demonstrates the following::
  • Google map using JavaScript
  • GeoComplete JavaScript for search address
  • Custom pin point for Google map
  • Travel mode and Route between pickup and drop off address
  • Calculate total distance & time to reach your destination

1. Initialize Map

  • Here I have created code for map initialization and accessing your location.
  • I wrote a method inside a document ready for map initialization.
  • The navigator method is asked for a sharing location that returns your current latitude and longitude.
  • Using lat and long we can render your current position on the map.
    1. $(document).ready(function()
    2. {
    3. //map initialize
    4. initialize();
    5. });
    6. function initialize()
    7. {
    8. //ask for share location
    9. navigator.geolocation.getCurrentPosition(doStuff, error, setOptions);
    10. function setOptions(geoLoc)
    11. {
    12. geoLoc.enableHighAccuracy = true;
    13. geoLoc.timeout = 30;
    14. geoLoc.maximumAge = 0;
    15. }
    16. function doStuff(geoLoc)
    17. {
    18. latlong = new google.maps.LatLng(geoLoc.coords.latitude, geoLoc.coords.longitude);
    19. geolatitude = geoLoc.coords.latitude;
    20. geolongitude = geoLoc.coords.longitude;
    21. var mapOptions = {
    22. center: latlong,
    23. zoom: 15,
    24. mapTypeId: google.maps.MapTypeId.ROADMAP
    25. };
    26. var image = 'img/home_alt.png';
    27. map = new google.maps.Map(document.getElementById('mapCanvas'), mapOptions);
    28. marker = new google.maps.Marker(
    29. {
    30. position: latlong,
    31. map: map,
    32. icon: image
    33. });
    34. }
    35. }
  • When you share location your screen looks like the following image:
    map

2. Search your location

  • I used geocomplete JS for the search location.
  • The following code is for search location and binding your location in textbox and label.
    1. $(function()
    2. {
    3. $("#PickupAddress")
    4. .geocomplete()
    5. .bind("geocode:result", function(event, result)
    6. {
    7. $("#spanPick").text($("#PickupAddress").val());
    8. pickUpLatLong = new google.maps.LatLng(result.geometry.location.lat(), result.geometry.location.lng());
    9. routeDirection("PickupAddress", pickUpLatLong);
    10. });
    11. });
3. Custom pinpoint & direction between pick up and drop off & total distance & time.
  • Here, I am taking an image variable for saving the custom image path and then attaching this image in the marker.
    1. function routeDirection(id, latLong)
    2. {
    3. geocoder = new google.maps.Geocoder();
    4. var mapOptions = {
    5. zoom: 15,
    6. center: latLong,
    7. mapTypeId: google.maps.MapTypeId.ROADMAP
    8. };
    9. map = new google.maps.Map(document.getElementById('mapCanvas'), mapOptions);
    10. var start = $("#PickupAddress").val();
    11. var end = $("#DropOffAddress").val();
    12. if (start != null && (end == null || end == ''))
    13. {
    14. geocoder.geocode(
    15. {
    16. "address": start
    17. }, function(results, status)
    18. {
    19. var image;
    20. if (status == google.maps.GeocoderStatus.OK)
    21. {
    22. image = 'img/pin_pickup_location.png';
    23. map = new google.maps.Map(document.getElementById('mapCanvas'), mapOptions);
    24. marker = new google.maps.Marker(
    25. {
    26. position: latLong,
    27. map: map,
    28. icon: image
    29. });
    30. }
    31. });
    32. }
    33. }
  • For getting direction from pick up address to drop off address we implement directionsService method.
  • If we get response status OK from Google map, then we can set direction on the map using JSON.
    1. directionsService.route(request, function(response, status)
    2. {
    3. if (status == google.maps.DirectionsStatus.OK)
    4. {
    5. directionsDisplay.setDirections(response);
    6. makeMarker(leg.start_location, "pickup location");
    7. makeMarker(leg.end_location, "dropoff location");
    8. }
    9. });
  • After getting OK status we get response JSON from Google that contains travel mode, distance, time, start location, and end location.
    1. var leg = response.routes[0].legs[0];
    2. //for distance
    3. $("#spanDist").text(leg.distance.text);
    4. //for time
    5. $("#spanTime").text(leg.duration.text);
    6. //for travel mode
    7. $("#spanTravelMode").text(response.request.travelMode);

4. Output

  • Test the output and click the following link.
    output