2015-10-08 107 views
0

我使用下面的代码为我的网页,但我想地图显示在this example边框。
我怎么弄出来的?谷歌地图的网页

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
<script language="javascript" type="text/javascript"> 

var map; 
var geocoder; 
function InitializeMap() { 

    var latlng = new google.maps.LatLng(39.646859, 27.883121); 
    var myOptions = 
    { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     disableDefaultUI: true 
    }; 
    map = new google.maps.Map(document.getElementById("map"), myOptions); 
} 

function FindLocation(whichone) { 
    geocoder = new google.maps.Geocoder(); 
    InitializeMap(); 

    var control1 = document.getElementById('<%= DDL_Ilce.ClientID %>'); 
    var selectedvalue1 = control1.options[control1.selectedIndex].text; 
    var control2 = document.getElementById('<%= DDL_Mahalle.ClientID %>'); 
    var selectedvalue2 = control2.options[control2.selectedIndex].text; 
    if (whichone == 1) { 
     var address = " Balıkesir" ; 
    } 
    else 
    { 
     var address = selectedvalue2 + " Balıkesir" ; 
    } 
    alert(address); 
    geocoder.geocode({ 'address': address, 'region':'Turkey' }, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.fitBounds(results[0].geometry.bounds); 
      var marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 

      }); 
     } 
     else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 

} 

window.onload = InitializeMap; 

</script> 

回答

0

添加Polyline方法。作为一个例子发生这样的事情你InitializeMap()函数中:

var flightPlanCoordinates = [ 
            {lat: 39.646859, lng: 27.883121}, 
            {lat: 40.646859, lng: 28.883121}, 
            {lat: 42.646859, lng: 30.883121}, 
            {lat: 43.646859, lng: 32.883121}, 
            {lat: 39.646859, lng: 27.883121} 
           ]; 
     var flightPath = new google.maps.Polyline({ 
           path: flightPlanCoordinates, 
           geodesic: true, 
           strokeColor: '#FF0000', 
           strokeOpacity: 1.0, 
           strokeWeight: 2 
         }); 

    flightPath.setMap(map); 

所以你的函数看起来像现在这样:

function InitializeMap(){ 
     var latlng = new google.maps.LatLng(39.646859, 27.883121); 
     var myOptions = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP, disableDefaultUI: true }; 
     map = new google.maps.Map(document.getElementById("map"), myOptions); 

     var flightPlanCoordinates = [ 
            {lat: 39.646859, lng: 27.883121}, 
            {lat: 40.646859, lng: 28.883121}, 
            {lat: 42.646859, lng: 30.883121}, 
            {lat: 43.646859, lng: 32.883121}, 
            {lat: 39.646859, lng: 27.883121} 
           ]; 
     var flightPath = new google.maps.Polyline({ 
           path: flightPlanCoordinates, 
           geodesic: true, 
           strokeColor: '#FF0000', 
           strokeOpacity: 1.0, 
           strokeWeight: 2 
         }); 

    flightPath.setMap(map); 
} 

你可以阅读更多关于它在这里: https://developers.google.com/maps/documentation/javascript/shapes

+0

GoogleMap会自动显示多边形。我希望它显示在每个地址搜索。 –

+0

您只需更新纬度并登录搜索。 – W3BGUY

+0

这没什么,但我可以在用户下次访问后在哪里获得新的flightPlanCoordinates。 –