【问题标题】:Get latlng and draw a polyline between that two latlng获取 latlng 并在这两个 latlng 之间绘制一条折线
【发布时间】:2013-06-26 22:56:44
【问题描述】:

我的代码中有一些问题,首先我想从两个文本框中获取给定地址/城市名称的 latlng,然后我将其转换为位置的 latlng 和位置的 latlng,最后我想画一个这些点和两个点上的标记之间的折线也是,但现在我试图在这些点之间画一条线。但是仍然无法使用此代码,控制台中也没有任何错误。代码在这里为您提供帮助。

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);
    // });
    }

}

【问题讨论】:

标签: html google-maps-api-3 cordova


【解决方案1】:

一个问题是

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

from_latlng 和 to_latlng 已经是 google.maps.LatLng 对象。

【讨论】:

  • 是的,你是对的,我改变了它并更新了问题
【解决方案2】:
/*This is script*/ 

    var x=0;
    var map;
    var prepath;
    var path = null;
    var current_lat = 28.6667;
    var current_lng = 77.2167;
function initialize() {
  var myLatLng = new google.maps.LatLng(28.6667, 77.2167);
  var mapOptions = {
    zoom: 3,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    //alert("sdf")  

google.maps.event.addListener(map, "click", function (e) {  
    //delete old
        //alert("sdf123")
    prepath = path;
    if(prepath){
        prepath.setMap(null);
    }   
    current_lat = e.latLng.lat();
    current_lng = e.latLng.lng();

    var flightPlanCoordinates = [               
            new google.maps.LatLng(39.9100, 116.4000),              
            new google.maps.LatLng(35.6833, 139.7667)               
    ];
    var polyline;   
    polyline = new google.maps.Polyline({
        path: flightPlanCoordinates,
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 2
    }); 
    //new polyline
    polyline.setMap(map);   
    // assign to global var path
    path = polyline;
    });

}


google.maps.event.addDomListener(window, 'load', initialize);


/*this is html*/

<div id="map-canvas"></div>
    <div id="textboxContainer" style="width:100%; height:40px; top:0; position:absolute;">
        From : <input type="text" name="travelFrom" id="travelFrom"> &nbsp; To : <input type="text" name="travelTo" id="travelTo" onBlur="getRoute()">
    </div>

【讨论】:

  • 这是一个完整的工作示例,您需要在 html 中添加 html 并在脚本标签之间添加脚本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 2017-11-24
  • 2011-07-24
  • 2013-07-08
相关资源
最近更新 更多