본문 바로가기

Dev/개발Tips

[개발Tip] 구글맵 지명/주소로 위경도 알아내기

반응형

특정 지명이나 주소 검색등으로 구글의 위/경도를 알아낼 수 있다.


<title>Google Map Sample</title>
  <script src="http://maps.google.com/maps?file=api&v=2&hl=&key=" type="text/javascript"></script>
    <script type="text/javascript">
function fnAdjust()
{
var latitude = document.googleMap.latitude.value;
var longitude = document.googleMap.longitude.value;
alert(latitude);
alert(longitude);
}
   
function load() {
  if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GOverviewMapControl());
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new GScaleControl()); 
map.enableDoubleClickZoom();
var center = new GLatLng(35.68205, 139.76789);
map.setCenter(center, 15, G_HYBRID_MAP);
geocoder = new GClientGeocoder();
var marker = new GMarker(center, {draggable: true});  
map.addOverlay(marker);
document.getElementById("lat").value = center.lat().toFixed(5);
document.getElementById("lng").value = center.lng().toFixed(5);
   
  GEvent.addListener(marker, "dragend", function() {
   var point = marker.getPoint();
  map.panTo(point);
   document.getElementById("lat").value = point.lat().toFixed(5);
   document.getElementById("lng").value = point.lng().toFixed(5);
   
});
   
   
 GEvent.addListener(map, "moveend", function() {
  map.clearOverlays();
var center = map.getCenter();
  var marker = new GMarker(center, {draggable: true});
  map.addOverlay(marker);
  document.getElementById("lat").value = center.lat().toFixed(5);
   document.getElementById("lng").value = center.lng().toFixed(5);
   
   
 GEvent.addListener(marker, "dragend", function() {
  var point =marker.getPoint();
 map.panTo(point);
  document.getElementById("lat").value = point.lat().toFixed(5);
 document.getElementById("lng").value = point.lng().toFixed(5);
   
});
   
});
   
  }
}
   
   function showAddress(address) {
   var map = new GMap2(document.getElementById("map"));
   map.addControl(new GSmallMapControl());
   map.addControl(new GMapTypeControl());
   if (geocoder) {
geocoder.getLatLng(
  address,
  function(point) {
if (!point) {
  alert(address + " not found");
} else {
  document.getElementById("lat").value = point.lat().toFixed(5);
   document.getElementById("lng").value = point.lng().toFixed(5);
 map.clearOverlays()
map.setCenter(point, 14);
   var marker = new GMarker(point, {draggable: true});  
 map.addOverlay(marker);
   
GEvent.addListener(marker, "dragend", function() {
  var pt = marker.getPoint();
 map.panTo(pt);
  document.getElementById("lat").value = pt.lat().toFixed(5);
 document.getElementById("lng").value = pt.lng().toFixed(5);
});
   
   
 GEvent.addListener(map, "moveend", function() {
  map.clearOverlays();
var center = map.getCenter();
  var marker = new GMarker(center, {draggable: true});
  map.addOverlay(marker);
  document.getElementById("lat").value = center.lat().toFixed(5);
   document.getElementById("lng").value = center.lng().toFixed(5);
   
 GEvent.addListener(marker, "dragend", function() {
 var pt = marker.getPoint();
map.panTo(pt);
document.getElementById("lat").value = pt.lat().toFixed(5);
   document.getElementById("lng").value = pt.lng().toFixed(5);
});
   
});
   
}
  }
);
  }
}
    </script>
   
   
    
 
<div style="padding: 15px 45px 10px;">
<div id="map" style="width: 600px; height: 500px;"></div>
</div>
     
<div style="padding: 5px 45px 10px 85px;">
<form name="googleMap" onsubmit="showAddress(this.address.value); return false" action="#">
 <table width="650">
  <tbody><tr>
<td colspan="5">
  <input name="address" type="text" size="70" value="">
  <input class="btn" type="submit" value="Address Search">
</td>
  </tr>
  <tr height="10">
<td colspan="5"></td>
  </tr>
  <tr>
<td width="117"><b>Latitude</b></td>
<td width="117">
  <input name="latitude" id="lat" type="text" size="12" value="">
</td>
<td width="117"><b>Longitude</b></td>
<td width="117">
  <input name="longitude" id="lng" type="text" size="12" value="">
</td>
<td width="182">
  <input class="btn" onclick="fnAdjust()" type="button" value="Adjust">
</td>
  </tr>
</tbody></table>
</form>
</div>
반응형