2014-03-25 48 views
2

我在尋找一些幫助。我有以下谷歌地圖代碼工作正常。我需要的只是一個起點上的自定義標記:(51.943382,6.463116),在路標上沒有標記。 可以這樣做嗎?我已經嘗試了幾種來自Stackoverflow的解決方案,但我對GMs代碼和編程技巧的理解還不足以獲得我想要的。 謝謝!谷歌地圖中的自定義和航點標記

<!DOCTYPE html> 
<html> 
    <head> 

     <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
     <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
     <title>Google Maps JavaScript API v3 Example: Directions Waypoints</title> 

     <link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" /> 
     <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> 
     </script> 
     <script type="text/javascript"> 
      var directionDisplay; 
      var directionsService = new google.maps.DirectionsService(); 
      var map; 

      function initialize() { 
       directionsDisplay = new google.maps.DirectionsRenderer({ 
       }); 


       var myOptions = { 
        zoom: 10, 
        mapTypeId: google.maps.MapTypeId.ROADMAP, 
       } 


       map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
       directionsDisplay.setMap(map); 
       calcRoute(); 
      } 

      function calcRoute() { 

       var waypts = []; 


       stop = new google.maps.LatLng(51.943571, 6.463856) 
         waypts.push({ 
          location:stop, 
          stopover:true}); 
       stop = new google.maps.LatLng(51.945032, 6.465776) 
         waypts.push({ 
          location:stop, 
          stopover:true}); 
       stop = new google.maps.LatLng(51.945538, 6.469413) 
         waypts.push({ 
          location:stop, 
          stopover:true}); 
       stop = new google.maps.LatLng(51.947462, 6.467941) 
         waypts.push({ 
          location:stop, 
          stopover:true}); 
       stop = new google.maps.LatLng(51.945409, 6.465562) 
         waypts.push({ 
          location:stop, 
          stopover:true}); 
       stop = new google.maps.LatLng(51.943700, 6.462096) 
         waypts.push({ 
          location:stop, 
          stopover:true}); 

       start = new google.maps.LatLng(51.943382, 6.463116); 
       end = new google.maps.LatLng(51.943382, 6.463116); 

       var request = { 
        origin: start, 
        destination: end, 
        waypoints: waypts, 
        optimizeWaypoints: true, 
        travelMode: google.maps.DirectionsTravelMode.WALKING 
       }; 

       directionsService.route(request, function(response, status) { 
        if (status == google.maps.DirectionsStatus.OK) { 
         directionsDisplay.setDirections(response); 
         var route = response.routes[0]; 

        } 
       }); 
      } 
     </script> 
    </head> 
    <body onLoad="initialize()"> 
     <div id="map_canvas" style="width:70%;height:80%;"></div> 
    </body> 
</html> 

回答

5

我看到只有一種解決方案來實現這一點。使用下面的代碼刪除所有標記:

directionsDisplay = new google.maps.DirectionsRenderer({ 
    suppressMarkers: true 
}); 

並在您的起始點手動添加標記。

這裏是一個JSFiddle來說明。

希望這會有所幫助!

0

只需添加您自己的標記而不依賴於方向。

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

這是假設您將此添加到您的初始化函數,以便訪問地圖。否則,您需要將您的映射變量設爲全局變量,以便可以在您的calcRoute函數中訪問它。

而在你directionsDisplay對象,設置suppressMarkers爲true:

directionsDisplay = new google.maps.DirectionsRenderer({ 
    suppressMarkers : true 
}); 

https://developers.google.com/maps/documentation/javascript/reference#DirectionsRendererOptions

相關問題