Witam,

W jaki sposób mogę za pomocą map.setCenter() wyśrodkować mapę nie na podstawie współrzędnych, ale na podstawie adresu typu: "Kłodzko,Polska".

Z góry dziękuję.


Kod:

  1. <script src="http://maps.google.com/maps?file=api&v=2&&hl=pl;key=ABQIAAAAYxpy0HiKBWXiyhVrpVqkshTwsqgIYL1WKUo2NH5PBQbiwgJSFhTsE-QuCFGPqmlghJQOzoJQiiJYnA" type="text/javascript"></script>
  2. <style type="text/css">
  3. body {
  4. font-family: Verdana, Arial, sans serif;
  5. font-size: 11px;
  6. margin: 2px;
  7. }
  8. table.directions th {
  9. background-color:#EEEEEE;
  10. }
  11.  
  12. img {
  13. color: #000000;
  14. }
  15. </style>
  16. <script type="text/javascript">
  17.  
  18. var map;
  19. var gdir;
  20. var geocoder = null; // ?
  21. var addressMarker; // ?
  22.  
  23.  
  24. function initialize() {
  25. if (GBrowserIsCompatible()) {
  26. map = new GMap2(document.getElementById("map_canvas"));
  27. gdir = new GDirections(map, document.getElementById("directions"));
  28. GEvent.addListener(gdir, "load", onGDirectionsLoad);
  29. GEvent.addListener(gdir, "error", handleErrors);
  30. GEvent.addListener(gdir, "addoverlay", onGDirectionsAddOverlay); // added to trigger marker swap
  31.  
  32.  
  33.  
  34. map.setCenter(new GLatLng(50.297046,16.652175),14); //Inital setCenter() added. Esa.
  35. map.addControl(new GLargeMapControl ());
  36. map.addControl(new GOverviewMapControl());
  37. map.addControl(new GScaleControl());
  38. map.addControl(new GMapTypeControl());
  39. map.hideControls();
  40. GEvent.addListener(map, "mouseover", function(){map.showControls();});
  41. GEvent.addListener(map, "mouseout", function(){map.hideControls();});
  42. // setDirections("Warszawa", "Poland", "pl_PL");
  43.  
  44. }
  45. }
  46.  
  47. function setDirections(fromAddress, toAddress, locale) {
  48. gdir.load("from: " + fromAddress + " to: " + toAddress,
  49. { "locale": locale , "getSteps":true});
  50. }
  51.  
  52. function handleErrors(){
  53. if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
  54. alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
  55. else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
  56. alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
  57.  
  58. else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
  59. alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
  60.  
  61. // else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug... this is either not defined, or Doc is wrong
  62. // 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);
  63.  
  64. else if (gdir.getStatus().code == G_GEO_BAD_KEY)
  65. alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
  66.  
  67. else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
  68. alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
  69.  
  70. else alert("An unknown error occurred.");
  71.  
  72. }
  73.  
  74. function onGDirectionsLoad(){
  75. // Use this function to access information about the latest load() results.
  76. }
  77.  
  78.  
  79. ///////////////////////////////////////////////////////////////////////
  80. // The add-on code for draggable markers
  81. // Esa 2008
  82. //
  83. var newMarkers = [];
  84. var latLngs = [];
  85. var icons = [];
  86.  
  87. // Note the 'addoverlay' GEvent listener added inside initialize() function
  88.  
  89. function onGDirectionsAddOverlay(){
  90. // Remove the draggable markers from previous function call.
  91. for (var i=0; i<newMarkers.length; i++)
  92. {map.removeOverlay(newMarkers[i]);
  93. }
  94.  
  95. // Loop through the markers and create draggable copies
  96. for (var i=0; i<=gdir.getNumRoutes(); i++)
  97. {
  98. var originalMarker = gdir.getMarker(i);
  99. latLngs[i] = originalMarker.getLatLng();
  100. icons[i] = originalMarker.getIcon();
  101. newMarkers[i] = new GMarker(latLngs[i],{icon:icons[i], draggable:true, title:'Draggable'});
  102. map.addOverlay(newMarkers[i]);
  103.  
  104. // Get the new waypoints from the newMarkers array and call loadFromWaypoints by dragend
  105. GEvent.addListener(newMarkers[i], "dragend", function()
  106. {
  107. var points = [];
  108. for (var i=0; i<newMarkers.length; i++)
  109. {
  110. points[i]= newMarkers[i].getLatLng();
  111. }
  112. gdir.loadFromWaypoints(points);
  113. });
  114.  
  115. //Bind 'click' event to original hidden marker
  116. copyClick(newMarkers[i],originalMarker);
  117.  
  118. // hide or remove the original marker
  119. //originalMarker.hide();
  120. map.removeOverlay(originalMarker);
  121. }
  122.  
  123. function copyClick(newMarker,oldMarker){
  124. GEvent.addListener(newMarker, 'click', function()
  125. {GEvent.trigger(oldMarker,'click');
  126. });
  127. }
  128. // End of draggable markers code
  129. // Esa 2008
  130. ///////////////////////////////////////////////////////////////////////
  131. }
  132.  
  133.  
  134. </head>
  135. <body onload="initialize()" onunload="GUnload()">
  136.  
  137. <h2>Maps API Directions Illustrated</h2>
  138. <form action="#" onsubmit="setDirections(this.from.value, this.to.value, this.locale.value); return false">
  139.  
  140.  
  141. <tr><th align="right">From: </th>
  142.  
  143. <td><input type="text" size="25" id="fromAddress" name="from"
  144. value="San Francisco"/></td>
  145. <th align="right">  To: </th>
  146. <td align="right"><input type="text" size="25" id="toAddress" name="to"
  147. value="Mountain View" /></td></tr>
  148.  
  149. <tr><th>Language: </th>
  150. <td colspan="3"><select id="locale" name="locale">
  151.  
  152. <option value="pl" selected>English</option>
  153.  
  154. <option value="fr">French</option>
  155.  
  156. <option value="de">German</option>
  157. <option value="ja">Japanese</option>
  158. <option value="es">Spanish</option>
  159. </select>
  160.  
  161. <input name="submit" type="submit" value="Get Directions!" />
  162.  
  163. </td></tr>
  164. </table>
  165.  
  166.  
  167. </form>
  168.  
  169. <br/>
  170. <table class="directions">
  171. <tr><th>Formatted Directions</th><th>Map</th><th>Draggable addOn by Esa</tr>
  172.  
  173. <tr>
  174. <td valign="top"><div id="directions" style="width: 275px"></div></td>
  175. <td valign="top"><div id="map_canvas" style="width: 310px; height: 400px;background: url('loader.gif'); background-repeat: no-repeat; background-position: center;"></div></td>
  176. </tr>
  177.  
  178. </table>
  179. </body>
  180. </html>