2012-06-11 3 views
0

다음 코드를 사용하여지도와 디스플레이를 호출합니다. 역 지오 코드를 수행하고 해당 번지를 출력하는 함수를 추가하고 싶습니다. 함수에 long/lat 좌표를 전달할 수 없습니다. 긴/위도 좌표를 도로 주소로 변환하려면 어떻게해야합니까?지오 코드 리버스

function getMyLocation() { 
    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(
     displayLocation, displayError, { 
      enableHighAccuracy: true, 
      timeout: 9000 
     }); 

     var watchButton = document.getElementById("watch"); 
     watchButton.onclick = watchLocation; 
     var clearWatchButton = document.getElementById("clearWatch"); 
     clearWatchButton.onclick = clearWatch; 
    } 
    else { 
     alert("Oops, no geolocation support"); 
    } 
} 

function displayLocation(position) { 
    var latitude = position.coords.latitude; 
    var longitude = position.coords.longitude; 

    var div = document.getElementById("location"); 
    div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude; 
    div.innerHTML += " (with " + position.coords.accuracy + " meters accuracy)"; 

    var km = computeDistance(position.coords, ourCoords); 
    var distance = document.getElementById("distance"); 
    distance.innerHTML = "You are " + km + " km from the WickedlySmart HQ"; 

    if (map == null) { 
     showMap(position.coords); 
     prevCoords = position.coords; 
    } 
    else { 
     var meters = computeDistance(position.coords, prevCoords) * 1000; 
     if (meters > 20) { 
      scrollMapToPosition(position.coords); 
      prevCoords = position.coords; 
     } 
    } 
}​ 

답변

0

나머지 부분은 시계 버튼과 관련이 없으며 나머지는 Chrome에서 잘 작동합니다.

file : // URL에서이 작업을 수행하려하지 않습니다.

+0

예 로저 위에서 작동하고 일을. 길항 정보를 역 지오 코드 함수로 푸시하고 거리 주소를 가져 오는 기능을 추가하려고합니다. 나는 그 거리 주소를 표시 할 것이다. 이것이 제가 도움이 필요한 부분입니다. 거리 주소를 얻는 방법. – user1449862

0

죄송합니다. 질문을 오해했습니다. 몇 주 전에 내가 프로젝트에 함께 사용한 몇 가지 코드가 있습니다. 그냥 우편 번호를 가져 오지만 주소를 얻을 수 있습니다. jQuery 용으로 작성되었습니다. 또한 Google Maps API를 동적으로로드하고 있습니다. 원하는 경우 직접로드 할 수 있습니다. 당신은 추가 라이브러리를 사용하는 게임을 경우

$(document).ready(function() { 
    if (navigator.geolocation) { 
     var script = document.createElement("script"); 
     script.type = 'text/javascript'; 
     script.src = 'http://www.google.com/jsapi?callback=loadMapsAPI'; 
     document.getElementsByTagName('head')[0].appendChild(script); 
    } else { 
     console.log('NOTE: Browser does not support geolocation.'); 
    } 
}); 


function loadMapsAPI() { 
    google.load('maps', '3', { 
     'other_params': 'sensor=false', 
     'callback' : mapLoaded 
     } 
    ); 
} 


function mapLoaded() { 
    $('#geolocateButton').css('display','block'); 
} 


function findMyLocation() { 
    navigator.geolocation.getCurrentPosition(
     function(position) { 
      var latLng = new google.maps.LatLng(
           position.coords.latitude, 
           position.coords.longitude); 
      var coder = new google.maps.Geocoder(); 
      coder.geocode({ 'latLng': latLng }, fillZipcode); 
     }, 
     function() { 
      alert("Could not determine your position."); 
     } 
    ); 
} 


function fillZipcode(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     if (results[1]) { 
      var parts = results[1].address_components; 
      for(var i = 0; i < parts.length; i++) { 
       if(parts[i].types[0] == 'postal_code') { 
        alert('Your Zip Code is: ' + parts[i].short_name); 
        return; 
       } 
      } 
     } else { 
      alert("Could not figure out your zip code."); 
     } 
    } else { 
     alert("Geocoder failed due to: " + status); 
    } 
} 
0

, 당신은 geous을 확인하고 jQuery 플러그인 here에 대한 최종 지오 코딩/위치 예를 들어 있습니다.

예는 setLocation 방법을 사용하여 양식을 autopopulates,하지만 당신은 그냥 간단하게 위치를 인쇄 할 수 있습니다 :

// location geocoded -- print address 
var onGeocodeSuccess = function (location) { 
    console.log(location.toAddress()); 
} 

// location retrieved -- geocode it. 
var onUserLocationSuccess = function (location) { 
    geous.geocode (location, { 
    reverse: true, 
    success: onGeocodeSuccess 
    }); 
} 

// Try and get user location 
geous.getUserLocation({ 
    success: onUserLocationSuccess 
}); 
관련 문제