2012-11-18 73 views
0

我试图在两个坐标之间绘制多段线时,在使用Google Maps API v3的setMap函数时遇到问题。getMap无法运行 - Google Maps API v3

我检查了代码与alert()失败的地方,它似乎在setMap函数中。有没有人有任何想法,为什么会发生这种情况?

function showPosition(position) 
    { 
    lat=position.coords.latitude; 
    lon=position.coords.longitude; 
    speed= position.coords.speed; 
    altitude = position.coords.altitude; 


    latlon=new google.maps.LatLng(lat, lon, speed, altitude) 
    mapholder=document.getElementById('mapholder') 
    mapholder.style.height='250px'; 
    mapholder.style.width='500px'; 

    var myOptions={ 
    center:latlon,zoom:14, 
    mapTypeId:google.maps.MapTypeId.ROADMAP, 
    mapTypeControl:false, 
    navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL} 
    }; 
    var map=new google.maps.Map(document.getElementById("mapholder"),myOptions); 
    var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"}); 


    x.innerHTML="Latitude: " + lat + " Long: " + lon; 
    mapRoute(); 

    } 

function mapRoute(showPosition, position){ 


var routeCoordinates = [ 
    new google.maps.LatLng(53.379763, -1.4658453999999999), 
    new google.maps.LatLng(21.291982, -157.821856) 
    ]; 



    var flightPath = new google.maps.Polyline({ 
    path: routeCoordinates, 
    strokeColor: "#FF0000", 
    strokeOpacity: 1.0, 
    strokeWeight: 2 
    }); 


flightPath.setMap(map); 


} 

由于提前, 马特

回答

1

这是一个范围问题

var map被内部限定的function showPosition(position)而不是作为一个全局变量

添加var map;上述任何函数调用并从var map=new google.maps.Map(document.getElementById("mapholder"),myOptions);删除var

那么你应该可以访问map里面flightPath.setMap(map);

+0

没问题很高兴我可以帮忙 –