webForumDet fria alternativet

Skriva ut kordinater i Google Maps

JavaScript

2 svar · 2 463 visningar · startad av eriks

Medlem sedan apr. 20011 418 inlägg
Frågan#1

Hej,

Jag tänkte sätta igång med google-maps, men får ett problem som grundar sig i att jag inte kan javascript tillräckligt. Nedan är en kod som till stor del kommer från Google AJAX APIs Playground

Jag vill göra precis det som koden säger, fast jag vill även skriva ut koordinaterna som en text på sidan. Jag förstår dock inte hur jag skriver ut detta. Ska jag skriva ut det i funktionen? Jag har testat att lägga till document.write(point) längst ner, och då skrivs det ut fint, men resten av funktionen laddas aldrig klart... Eller måste jag göra en egen funktion för detta?

Är som sagt mycket förvirrad och lite hjälp på traven hade varit mycket uppskattat!

Mvh Erik

Script:

var map;
var geocoder;

function initialize() {
 map = new GMap2(document.getElementById("map_canvas"));
 map.setCenter(new GLatLng(34, 0), 1);
 geocoder = new GClientGeocoder();
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
 map.clearOverlays();
 if (!response || response.Status.code != 200) {
   alert("Sorry, we were unable to geocode that address");
 } else {
   place = response.Placemark[0];
   point = new GLatLng(place.Point.coordinates[1],
                       place.Point.coordinates[0]);
   marker = new GMarker(point);
   map.addOverlay(marker);
   marker.openInfoWindowHtml(place.address + '<br>' +
     '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
   return point;
 }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
 var address = document.forms[0].q.value;
 geocoder.getLocations(address, addAddressToMap);
}
<body onload="initialize()" onunload="GUnload()">

 <form action="#" onsubmit="showLocation(); return false;">
      <p>
	  
        <b>Search for an address:</b>
        <input type="text" name="q" value="" class="address_input" size="40" />
        <input type="submit" name="find" value="Search" />

      </p>
    </form>
<br/>

    <div id="map_canvas" style="width: 500px; height: 300px"></div>
  </body>
Medlem sedan mars 20007 896 inlägg
#2
var map;
var geocoder;

function initialize() {
 map = new GMap2(document.getElementById("map_canvas"));
 map.setCenter(new GLatLng(34, 0), 1);
 geocoder = new GClientGeocoder();
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
 map.clearOverlays();
 if (!response || response.Status.code != 200) {
   alert("Sorry, we were unable to geocode that address");
 } else {
   place = response.Placemark[0];
   [b]document.getElementById('latitude').innerHTML = place.Point.coordinates[1];
   document.getElementById('longitude').innerHTML = place.Point.coordinates[0];[/b]
   point = new GLatLng(place.Point.coordinates[1],
                       place.Point.coordinates[0]);
   marker = new GMarker(point);
   map.addOverlay(marker);
   marker.openInfoWindowHtml(place.address + '<br>' +
     '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
   return point;
 }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
 var address = document.forms[0].q.value;
 geocoder.getLocations(address, addAddressToMap);
}
<body onload="initialize()" onunload="GUnload()">

 <form action="#" onsubmit="showLocation(); return false;">
      <p>
	  
        <b>Search for an address:</b>
        <input type="text" name="q" value="" class="address_input" size="40" />
        <input type="submit" name="find" value="Search" />

      </p>
    </form>

[b]<span id="latitude"></span><br>
<span id="longitude"></span>[/b]

<br/>

    <div id="map_canvas" style="width: 500px; height: 300px"></div>
  </body>

:?

Medlem sedan apr. 20011 418 inlägg
#3

getElementById vad det alltså. Fungerar fint, tack för det!

Mvh Erik

262 ms totalt · 4 externa anrop · v20260731065814-full.a51de22e
128 ms — deklarationer (db)
0 ms — hämta statistik (cache)
130 ms — hämta tråd, inlägg och bilagor (db)
129 ms — ändringar (db)