Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

html - Get latlng and draw a polyline between that two latlng

I have some issue in my code in that first I want to get latlng of my given address/city name from two text boxes after that i converts it to from position's latlng and to position's latlng and at last i want to draw a polyline between these point and markers on both point also, But now i am trying to draw a line between these points. But still not working this code also no any error in console also. code is here for your help.

function getRoute(){
var from_text = document.getElementById("travelFrom").value;
var to_text = document.getElementById("travelTo").value;

if(from_text == ""){
    alert("Enter travel from field")    
    document.getElementById("travelFrom").focus();
}
else if(to_text == ""){
    alert("Enter travel to field"); 
    document.getElementById("travelTo").focus();
}
else{
    //google.maps.event.addListener(map, "", function (e) {
    var myLatLng = new google.maps.LatLng(28.6667, 77.2167);                                                           
    var mapOptions = {
        zoom: 3,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };                                                         
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);                                                          
        var geocoder = new google.maps.Geocoder();
        var address1 = from_text;
        var address2 = to_text;
        var from_latlng,to_latlng;
            //var prepath = path;
            //if(prepath){
            //  prepath.setMap(null);
            //} 
            geocoder.geocode( { 'address': address1}, function(results, status) {
                  if (status == google.maps.GeocoderStatus.OK)
                  {
                      // do something with the geocoded result
                //    alert(results[0].geometry.location);
                      from_latlng = results[0].geometry.location;

                    //   from_lat = results[0].geometry.location.latitude;
                      // from_lng = results[0].geometry.location.longitude;

                    //  alert(from_latlng);
                  }
            });
            geocoder.geocode( { 'address': address2}, function(results, status) {
                  if (status == google.maps.GeocoderStatus.OK)
                  {
                      // do something with the geocoded result
                        to_latlng = results[0].geometry.location;
                     //  to_lat = results[0].geometry.location.latitude;
                     //  to_lng = results[0].geometry.location.longitude;
                      // results[0].geometry.location.longitude
                    //  alert(to_latlng)
                  }
            });
            setTimeout(function(){
                    var flightPlanCoordinates = [
                        new google.maps.LatLng(from_latlng),
                        new google.maps.LatLng(to_latlng)           
                    ];
                    //alert("123")
                    var polyline;   
                    polyline = new google.maps.Polyline({
                        path: flightPlanCoordinates,
                        strokeColor: "#FF0000",
                        strokeOpacity: 1.0,
                        strokeWeight: 2
                    });
                    polyline.setMap(map);       
                    // assign to global var path
                //  path = polyline;
         },4000);
    // });
    }

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One problem is

 var flightPlanCoordinates = [
       new google.maps.LatLng(from_latlng),
       new google.maps.LatLng(to_latlng)           
     ];

from_latlng and to_latlng are already google.maps.LatLng objects.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...