2012-04-02 86 views
1

我想在兩個位置之間搜索路線時更改標記的默認圖標。我該怎麼做?我用這個代碼,但沒有途徑我沒有得到的只是地圖,並指出更改路線中標記的默認圖標

var start = new google.maps.Marker({ 
position: new google.maps.LatLng(lat, lon), 
icon:'http://www.google.com/mapfiles/dd-start.png', 
map: map }); 

var end = new google.maps.Marker({ 
position: new google.maps.LatLng(lat1, long1), 
icon:'http://www.google.com/mapfiles/dd-end.png', 
map: map }); 
var request = { 
      origin: start, 
       destination: end, 
       optimizeWaypoints: true, 
      travelMode: google.maps.DirectionsTravelMode.DRIVING 
      }; 
+0

是否清楚現在?我改變了我的問題。我想更改默認圖標(帶有字母的標記)。我想在源代碼和目標位置用'http://www.google.com/mapfiles/dd-start.png'更改此圖標我想將此圖片http://www.google.com/ mapfiles/dd-end.png – mobileDeveloper 2012-04-02 15:41:43

+0

Aha,你的意思是代替使用默認圖標,你想使用自定義圖標。 – 2012-04-02 15:45:18

+0

是的,我想在搜索路線時使用自定義標記圖標 – mobileDeveloper 2012-04-02 15:46:24

回答

3

當您創建渲染器添加此選項,然後將您的標記在原點(開始)和目標(完)

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

這裏是工作示例,根據您的要求進行更改。

<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: Optimized Directions</title> 
<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; 
    var origin = null; 
    var destination = null; 
    var waypoints = []; 
    var markers = []; 
    var directionsVisible = false; 

    function initialize() { 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
    var chicago = new google.maps.LatLng(37.7749295, -122.4194155); 
    var myOptions = { 
     zoom:13, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: chicago 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    directionsDisplay.setMap(map); 
    directionsDisplay.setPanel(document.getElementById("directionsPanel")); 

    google.maps.event.addListener(map, 'click', function(event) { 
     if (origin == null) { 
     origin = event.latLng; 
     addMarker(origin); 
     } else if (destination == null) { 
     destination = event.latLng; 
     addMarker(destination); 
     } else { 
     if (waypoints.length < 9) { 
      waypoints.push({ location: destination, stopover: true }); 
      destination = event.latLng; 
      addMarker(destination); 
     } else { 
      alert("Maximum number of waypoints reached"); 
     } 
     } 
    }); 
    } 

    function addMarker(latlng) { 
    markers.push(new google.maps.Marker({ 
     position: latlng, 
     map: map, 
     icon: "http://maps.google.com/mapfiles/marker" + String.fromCharCode(markers.length + 65) + ".png" 
    }));  
    } 

    function calcRoute() { 

    if (origin == null) { 
     alert("Click on the map to add a start point"); 
     return; 
    } 

    if (destination == null) { 
     alert("Click on the map to add an end point"); 
     return; 
    } 

    var mode = google.maps.DirectionsTravelMode.DRIVING; 

    var request = { 
     origin: origin, 
     destination: destination, 
     waypoints: waypoints, 
     travelMode: mode, 

    }; 

    directionsService.route(request, function(response, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 
    directionsDisplay.setDirections(response); 
     } 
    }); 

    clearMarkers(); 
    directionsVisible = true; 
    } 

    function clearMarkers() { 
    for (var i = 0; i < markers.length; i++) { 
     markers[i].setMap(null); 
    } 
    } 

    function clearWaypoints() { 
    markers = []; 
    origin = null; 
    destination = null; 
    waypoints = []; 
    directionsVisible = false; 
    } 

    function reset() { 
    clearMarkers(); 
    clearWaypoints(); 
    directionsDisplay.setMap(null); 
    directionsDisplay.setPanel(null); 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
    directionsDisplay.setMap(map); 
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));  
    } 
</script> 
</head> 
<body onload="initialize()" style="font-family: sans-serif;"> 
    <table style="width: 400px"> 

    <tr> 
     <td><input type="button" value="Reset" onclick="reset()" /></td> 
    </tr> 
    <tr> 
     <td><input type="button" value="Get Directions!" onclick="calcRoute()" /></td> 
     <td></td> 
    </tr> 
    </table> 
    <div style="position:relative; border: 1px; width: 610px; height: 400px;"> 
    <div id="map_canvas" style="border: 1px solid black; position:absolute; width:398px; height:398px"></div> 
    <div id="directionsPanel" style="position:absolute; left: 410px; width:240px; height:400px; overflow: auto"></div> 

    </div> 
</body> 
</html> 
+0

這個例子不是我所需要的。當我點擊按鈕「get Direction」時,我想更改默認圖標的圖像。我得到相同的圖標,路徑中標記的默認圖標 – mobileDeveloper 2012-04-04 14:39:23

0

好吧,我試試:

  1. visible = falsedraggable = trueDirectionsRendererOptions對象
  2. 創建DirectionsRenderer對象
  3. 創建您自己的標記(與draggable=true選項)
  4. dragstartdrag事件從你的馬rker向渲染標記(開始和結束)

    google.maps.event.addListener(marker_start, 'dragstart', function(e) { 
        directionsRenderer.b.markers[0].setPosition(this.getPosition()); 
        google.maps.event.trigger(directionsRenderer.b.markers[0], 'dragstart', e); 
    }); 
    
    google.maps.event.addListener(marker_start, 'drag', function(e) { 
        directionsRenderer.b.markers[0].setPosition(this.getPosition()); 
        google.maps.event.trigger(directionsRenderer.b.markers[0], 'drag', e); 
    }); 
    
    google.maps.event.addListener(marker_end, 'dragstart', function(e) { 
        var l = directionsRenderer.b.markers.length - 1; 
        directionsRenderer.b.markers[l].setPosition(this.getPosition()); 
        google.maps.event.trigger(directionsRenderer.b.markers[l], 'dragstart', e); 
    }); 
    
    google.maps.event.addListener(marker_end, 'drag', function(e) { 
        var l = directionsRenderer.b.markers.length - 1; 
        directionsRenderer.b.markers[l].setPosition(this.getPosition()); 
        google.maps.event.trigger(directionsRenderer.b.markers[l], 'drag', e); 
    });