Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i have following code for that show latitude and longitude of particular place in infoWindow when click on that place...
means click on place A lat-log of A will be shown in infowindow and click on place B lat-log of B will be shown in infowindow and

but i want to show direction between those places..
how i can do this...plz give some idea...

thank u for help !!!
here is my code...

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
 <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script type="text/javascript">
 
  var  geocoder = new google.maps.Geocoder();
 var infowindow = new google.maps.InfoWindow();
var latlang=new google.maps.LatLng(geoip_latitude(), geoip_longitude());
  function initialize()
  {
      var myOptions = {
      center: latlang,
      zoom: 5,
      mapTypeId: google.maps.MapTypeId.ROADMAP
 
    };
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
 geocoder.geocode({ 'latLng': latlang }, function (items, status) {

                    if (status == google.maps.GeocoderStatus.OK) {
 
                        map.setZoom(8);
 
                       var marker = new google.maps.Marker({
                            position: latlang ,
                            map: map
                        });
 
                     infowindow.setContent(items[1].formatted_address);
                         google.maps.event.addListener(marker, 'click', function () {

 
                infowindow.open(map, this);
                map.setZoom(8);
            });
                    }
 
                });
google.maps.event.addListener(map, 'click', function (event) {

                geocoder.geocode({ 'latLng': event.latLng }, function (items, status) {

                    if (status == google.maps.GeocoderStatus.OK) {
                        $('#latitude').val(items[0].geometry.location.lat().toString().substr(0, 12));
                        $('#longitude').val(items[0].geometry.location.lng().toString().substr(0, 12));
                        map.setZoom(8);
                       marker = new google.maps.Marker({
                            position: event.latLng,
                            map: map
                        });
 
                        var ll=event.latLng;
                        infowindow.setContent(ll.toString());
                        infowindow.open(map, marker);
 
                    }
 
                });
            });
}
 

window.onload=initialize;
 
</script>
</head>
<body>
<div id="map" align="center" style="height: 420px;  width:500px; top:0px; border: 1px solid black;"></div>
</body>
</html>
Posted

Try this..
XML
<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
 <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script type="text/javascript">
 var directionsDisplay;
var directionsService = new google.maps.DirectionsService();

  var  geocoder = new google.maps.Geocoder();
 var infowindow = new google.maps.InfoWindow();
var latlang=new google.maps.LatLng(geoip_latitude(), geoip_longitude());
var start;var end;
  function initialize()
  {
 directionsDisplay = new google.maps.DirectionsRenderer();

      var myOptions = {
      center: latlang,
      zoom: 5,
      mapTypeId: google.maps.MapTypeId.ROADMAP

    };
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
 start=latlang;

 geocoder.geocode({ 'latLng': latlang }, function (items, status) {

                    if (status == google.maps.GeocoderStatus.OK) {

                        map.setZoom(8);

                       var marker = new google.maps.Marker({
                            position: latlang ,
                            map: map
                        });

                     infowindow.setContent(items[1].formatted_address);
                         google.maps.event.addListener(marker, 'click', function () {


                infowindow.open(map, this);
                map.setZoom(8);
            });
                    }

                });
google.maps.event.addListener(map, 'click', function (event) {

                geocoder.geocode({ 'latLng': event.latLng }, function (items, status) {

                    if (status == google.maps.GeocoderStatus.OK) {
                        $('#latitude').val(items[0].geometry.location.lat().toString().substr(0, 12));
                        $('#longitude').val(items[0].geometry.location.lng().toString().substr(0, 12));

                       marker = new google.maps.Marker({
                            position: event.latLng,
                            map: map
                        });

                        var ll=event.latLng;
                        infowindow.setContent(ll.toString());
                        infowindow.open(map, marker);


                    }

                });
end=event.latLng;
directionsDisplay.setMap(map);
var request = {
    origin:start,
    destination:end,
    travelMode: google.maps.TravelMode.DRIVING
  };
directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
    }
  });
start=event.latLng;
            });




}


window.onload=initialize;

</script>
</head>
<body>
<div id="map" align="center" style="height: 420px;  width:500px; top:0px; border: 1px solid black;"></div>
</body>
</html>
 
Share this answer
 
Guess direction service will help you get the direction between two points.

check this ...The Google Directions API and check this sample page from google direction services.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900