Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [inne]Zmiana ikonek na googlemaps
Forum PHP.pl > Forum > Przedszkole
northwest
Witam serdecznie,
Mam taki prosty skrypcik do wyświetlania trasy z punktu A do punktu B:
  1. <script type="text/javascript">
  2. var map;
  3. var gdir;
  4. var geocoder = null;
  5. var addressMarker;
  6. function printpage() {
  7. window.print();
  8. }
  9.  
  10. function initialize() {
  11. if (GBrowserIsCompatible()) {
  12. map = new GMap2(document.getElementById("map_canvas"));
  13. gdir = new GDirections(map, document.getElementById("directions"));
  14. GEvent.addListener(gdir, "load", onGDirectionsLoad);
  15. GEvent.addListener(gdir, "error", handleErrors);
  16. map.addControl(new GLargeMapControl ());
  17. map.addControl(new GScaleControl());
  18. map.addControl(new GMapTypeControl());
  19.  
  20. map.enableContinuousZoom();
  21. map.enableDoubleClickZoom();
  22. map.enableScrollWheelZoom();
  23. setDirections("Warszawa, iołka", "Warszawa, maczka", "pl");
  24. }
  25. }
  26.  
  27. function setDirections(fromAddress, toAddress, locale) {
  28. gdir.load("from: " + fromAddress + " to: " + toAddress,
  29. { "locale": locale });
  30. }
  31.  
  32. function handleErrors(){
  33. if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
  34. alert("Nie mozna odnalezc szukanej lokalizacji!");
  35. else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
  36. alert("Nie mozna odnalezc szukanej lokalizacji!");
  37.  
  38. else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
  39. alert("Nie mozna odnalezc szukanej lokalizacji!");
  40.  
  41. // else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug... this is either not defined, or Doc is wrong
  42. // alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
  43.  
  44. else if (gdir.getStatus().code == G_GEO_BAD_KEY)
  45. alert("Nie mozna odnalezc szukanej lokalizacji!");
  46. else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
  47. alert("Nie mozna odnalezc szukanej lokalizacji!");
  48. else alert("An unknown error occurred.");
  49.  
  50. }
  51.  
  52. function onGDirectionsLoad(){
  53.  
  54. }
  55. </script>
  56. </head>
  57. <body onload="initialize()">
  58. <h2>Mapa trasy</h2><br/>
  59. <form action="#" onsubmit="setDirections(this.from.value, this.to.value, this.locale.value); return false">
  60. <small><b>Zalecana forma wprowadzonego adresu:</b> Gdynia, Podgorska 22<br/><br/>
  61. <table>
  62. <tr>
  63. <td>
  64. <th align="right">Wyjazd z:&nbsp;</th>
  65. </td>
  66. <td>
  67. <input type="text" size="55" id="fromAddress" name="from" value="Warszawa, iołka"/>
  68. </td>
  69. </tr>
  70. <tr>
  71. <td>
  72. <th align="right">&nbsp;&nbsp;Adres docelowy:&nbsp;</th>
  73. </td>
  74. <td>
  75. <input type="text" size="55" id="toAddress" name="to" value="Warszawa, maczka" />
  76. </td>
  77. </tr>
  78. <tr>
  79. <td colspan="3"><input type ="hidden" id="locale" name="locale" value="pl">
  80. <input name="submit" type="submit" value="Pokaż trasę" />
  81. </td></tr>
  82. </table>
  83. </form>
  84. <br/>
  85. <div id="map_canvas" style="width: 95%; height: 400px"></div>
  86. <div id="directions" style="width: 95%"></div>
  87. </body>
  88. </html>


Wszystko pięknie chodzi :-) Chciałbym zmienić domyslną ikonke dla punktu A na np. myname.pl/a.png i dla punktu B na myname.pl/b.png...

Wiecie może jak to zrobić?

Z góry dziękuje za poomoc :-)
Northwest
Daimos
dokumentacja google maps api nie gryzie:
http://code.google.com/intl/pl/apis/maps/d...ml#Custom_Icons
przykład:
http://code.google.com/intl/pl/apis/maps/d...con-custom.html
northwest
To ja wiem, wklejam sobie definicję ikonek:
  1. var new_icon = new GIcon();
  2. new_icon.image = "http://myname.pl/a.png";
  3. new_icon.size = new GSize(16,16);
  4. new_icon.iconAnchor = new GPoint(8,9);
  5. new_icon.infoWindowAnchor = new GPoint(7,7);
  6.  
  7. var new_icon2 = new GIcon();
  8. new_icon2.image = "http://myname.pl/b.png";
  9. new_icon2.size = new GSize(16,16);
  10. new_icon2.iconAnchor = new GPoint(8,9);
  11. new_icon2.infoWindowAnchor = new GPoint(7,7);


ale jak to później w tym kodzie "przykleić" do tych punktów?
thek
Znowu dokumentacja? Nie będę jednak zbyt wiele dawał, tylko podam słowa kluczowe dla przeglądarki: Google maps directions marker icon change
northwest
udało mi się zmontować tą mapkę smile.gif

  1. <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=pl"></script>
  2. <script type="text/javascript">
  3. //<![CDATA[
  4. var side_bar_html = "";
  5. var gmarkers = [];
  6. var map = null;
  7. var startLocation = null;
  8. var endLocation = null;
  9.  
  10. function initialize() {
  11. var center = new google.maps.LatLng(24.7756,121.0062);
  12. map = new google.maps.Map(document.getElementById('map_canvas'), {
  13. center: center,
  14. zoom: 13,
  15. mapTypeId: google.maps.MapTypeId.ROADMAP
  16. });
  17.  
  18. google.maps.event.addListener(map, 'click', function() {
  19. infowindow.close();
  20. });
  21.  
  22. var directionsService = new google.maps.DirectionsService();
  23. var request = {
  24. origin: "Warszawa, Maczka",
  25. destination: "Warszawa, Ciołka",
  26. travelMode: google.maps.DirectionsTravelMode.WALKING
  27. };
  28.  
  29. var polyline = new google.maps.Polyline({
  30. path: [],
  31. strokeColor: '#FF0000',
  32. strokeWeight: 3
  33. });
  34.  
  35. directionsService.route(request, function(response, status) {
  36. if (status == google.maps.DirectionsStatus.OK) {
  37. var bounds = new google.maps.LatLngBounds();
  38. var route = response.routes[0];
  39. var summaryPanel = document.getElementById("directions_panel");
  40. var detailsPanel = document.getElementById("direction_details");
  41. startLocation = new Object();
  42. endLocation = new Object();
  43. summaryPanel.innerHTML = "";
  44. detailsPanel.innerHTML = '<ul>';
  45.  
  46. for (var i = 0; i < route.legs.length; i++) {
  47. var routeSegment = i + 1;
  48. summaryPanel.innerHTML += "<b>Plan trasy: " + routeSegment + "</b><br />";
  49. summaryPanel.innerHTML += route.legs[i].start_address + " do ";
  50. summaryPanel.innerHTML += route.legs[i].end_address + "<br />";
  51. summaryPanel.innerHTML += route.legs[i].distance.text + "<br /><br />";
  52. }
  53.  
  54. var path = response.routes[0].overview_path;
  55. var legs = response.routes[0].legs;
  56. for (i=0;i<legs.length;i++) {
  57. if (i == 0) {
  58. startLocation.latlng = legs[i].start_location;
  59. startLocation.address = legs[i].start_address;
  60. createMarker(legs[i].start_location,"start",legs[i].start_address,"green");
  61. }
  62. endLocation.latlng = legs[i].end_location;
  63. endLocation.address = legs[i].end_address;
  64. var steps = legs[i].steps;
  65. for (j=0;j<steps.length;j++) {
  66. var nextSegment = steps[j].path;
  67. detailsPanel.innerHTML += "<li>"+steps[j].instructions;
  68. var dist_dur = "";
  69. if (steps[j].distance && steps[j].distance.text) dist_dur += "&nbsp;"+steps[j].distance.text;
  70. if (steps[j].duration && steps[j].duration.text) dist_dur += "&nbsp;"+steps[j].duration.text;
  71. if (dist_dur != "") {
  72. detailsPanel.innerHTML += "("+dist_dur+")<br /></li>";
  73. } else {
  74. detailsPanel.innerHTML += "</li>";
  75. }
  76. for (k=0;k<nextSegment.length;k++) {
  77. polyline.getPath().push(nextSegment[k]);
  78. bounds.extend(nextSegment[k]);
  79. }
  80. }
  81. }
  82. detailsPanel.innerHTML += "</ul>"
  83. polyline.setMap(map);
  84. map.fitBounds(bounds);
  85. createMarker(endLocation.latlng,"end",endLocation.address,"red");
  86. // == create the initial sidebar ==
  87. makeSidebar();
  88. }
  89. });
  90. }
  91.  
  92. var icons = new Array();
  93. icons["red"] = new google.maps.MarkerImage("http://wafao.com/taxometr/postoj.png",
  94. new google.maps.Size(28, 31),
  95. new google.maps.Point(0,0),
  96. new google.maps.Point(28, 31));
  97.  
  98. function getMarkerImage(iconColor) {
  99. if ((typeof(iconColor)=="undefined") || (iconColor==null)) {
  100. iconColor = "red";
  101. }
  102.  
  103. if (!icons[iconColor]) {
  104. icons[iconColor] = new google.maps.MarkerImage("http://wafao.com/taxometr/taxi.png",
  105. // This marker is 20 pixels wide by 34 pixels tall.
  106. new google.maps.Size(48, 31),
  107. // The origin for this image is 0,0.
  108. new google.maps.Point(0,0),
  109. // The anchor for this image is at 6,20.
  110. new google.maps.Point(22, 20));
  111. }
  112. return icons[iconColor];
  113. }
  114.  
  115. var iconImage = new google.maps.MarkerImage('http://wafao.com/taxometr/taxi.png',
  116. // This marker is 20 pixels wide by 34 pixels tall.
  117. new google.maps.Size(48, 31),
  118. // The origin for this image is 0,0.
  119. new google.maps.Point(0,0),
  120. // The anchor for this image is at 9,34.
  121. new google.maps.Point(9, 34));
  122.  
  123. var iconShadow = new google.maps.MarkerImage('http://wafao.com/taxometr/taxi.png',
  124.  
  125. new google.maps.Size(37, 34),
  126. new google.maps.Point(0,0),
  127. new google.maps.Point(37, 34));
  128.  
  129. var iconShape = {
  130. coord: [9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0],
  131. type: 'poly'
  132. };
  133.  
  134. var infowindow = new google.maps.InfoWindow(
  135. {
  136. size: new google.maps.Size(150,50)
  137. });
  138.  
  139. function createMarker(latlng, label, html, color) {
  140. // alert("createMarker("+latlng+","+label+","+html+","+color+")");
  141. var contentString = '<b>'+label+'</b><br>'+html;
  142. var marker = new google.maps.Marker({
  143. position: latlng,
  144. map: map,
  145. // shadow: iconShadow,
  146. icon: getMarkerImage(color),
  147. shape: iconShape,
  148. title: label,
  149. zIndex: Math.round(latlng.lat()*-100000)<<5
  150. });
  151. marker.myname = label;
  152. gmarkers.push(marker);
  153.  
  154. google.maps.event.addListener(marker, 'click', function() {
  155. infowindow.setContent(contentString);
  156. infowindow.open(map,marker);
  157. });
  158. }
  159. //]]>
  160. </script>
  161. <style type="text/css">
  162. html { height: 100% }
  163. body { height: 100%; margin: 0px; padding: 0px }
  164. #map_canvas { height: 100% }
  165. </style>
  166. </head>
  167. <body onload="initialize()">
  168. <div id="map_canvas" style="float:left;width:70%;height:100%;"></div>
  169. <div id="control_panel" style="float:right;width:30%;text-align:left;padding-top:20px">
  170. <table border="1"><tr><td>
  171. <div id="directions_panel" style="margin:20px;background-color:#FFEE77;"></div>
  172. </td></tr><tr><td>
  173. <div id="direction_details" style="margin:20px;"></div>
  174. </td></tr></table>
  175. </div>
  176. <br/><br/>
  177. <form action="#" onsubmit="setDirections(this.from.value, this.to.value, this.locale.value); return false">
  178. <small><b>Zalecana forma wprowadzonego adresu:</b> Gdynia, Podgorska 22<br/><br/>
  179. <table>
  180. <tr>
  181. <td>
  182. <th align="right">Wyjazd z:&nbsp;</th>
  183. </td>
  184. <td>
  185. <input type="text" size="55" id="fromAddress" name="from" value=""/>
  186. </td>
  187. </tr>
  188. <tr>
  189. <td>
  190. <th align="right">&nbsp;&nbsp;Adres docelowy:&nbsp;</th>
  191. </td>
  192. <td>
  193. <input type="text" size="55" id="toAddress" name="to" value="" />
  194. </td>
  195. </tr>
  196. <tr>
  197. <td colspan="3"><input type ="hidden" id="locale" name="locale" value="pl">
  198. <input name="submit" type="submit" value="Pokaż trasę" />
  199. </td></tr>
  200. </table>
  201. </form>



Tylko nie wiem jak podłączyć ten formularz żeby mapka pokazywała trasę także po wpisaniu w nim adresów :-( Wiecie może jak to zrobić?
thek
Słowo kluczowe: geokodowanie smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.