2015-01-28 2 views
-1

Google Geolocation을 대단히 성공적으로 사용하고 있습니다. 어제는 사용자 위치의 경계에서 검색하여 storelocator 스크립트를 개선하기 위해 이동했습니다. 따라서 우리가 위치를 지정할 때 북동부 및 남서부의 위도와 경도 경계가 필요한 사용자가 필요합니다. 내가 궁금해 무엇, 경계를 얻을 수있는 간단한 방법은 Google Geolocation을 사용하여 경계를 얻는 것이 맞습니까?

$('#geomate').click(function() { 
var geocoder; 
var split1; 
if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction); 
} 
//Get the latitude and the longitude; 
function successFunction(position) { 
    var lat = position.coords.latitude; 
    var lng = position.coords.longitude; 
    codeLatLng(lat, lng); 
} 
function errorFunction(){ 
    alert("Geolocator failed. If you're on a mobile device, please switch on your Location settings (GPS)"); 
} 
geocoder = new google.maps.Geocoder(); 
function codeLatLng(lat, lng) { 
    var latlng = new google.maps.LatLng(lat, lng); 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      console.log(results) 
      if (results[1]) { 
       split1 = results[1].formatted_address.split(', '); 
       swlat = results[1].geometry.bounds.Ca.k; 
       swlng = results[1].geometry.bounds.va.j; 
       nelat = results[1].geometry.bounds.Ca.j; 
       nelng = results[1].geometry.bounds.va.k; 
       //formatted address 

       //if zipcode has a value, clear it first 
       if ($('input[name="zipcode"]').val() != '') { 
       $('input[name="zipcode"]').val(''); 
       } 
       //now enter the geolocation 
       $('input[name="zipcode"]').val(split1[1] + ', ' + split1[2]); 
       // if hidden geomtery fields exist, remove them 
       $('.storelocator_body form').find('.geomerty').remove(); 
       $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="lat" value="' + lat + '">'); 
       $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="lng" value="' + lng + '">'); 
       $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="swlat" value="' + swlat + '">'); 
       $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="swlng" value="' + swlng + '">'); 
       $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="nelat" value="' + nelat + '">'); 
       $('.storelocator_body form').append('<input type="hidden" class="geomerty" name="nelng" value="' + nelng + '">'); 
      } else { 
       alert('No results found'); 
      } 
     } else { 
      alert('Geocoder failed due to: ' + status); 
     } 
    }); 
} 

: (나는 경우에이 도움이 전체의 지리적 위치 설정 기능을 붙여 넣기 해요) 다음과 같이

우리는 콘솔에서 다음을 얻을 수있다 지구 위치 요청 에서요? 예를 들어, 다음 코드를 통해 경계를 얻기 위해 노력했다 ": swLatLn이 정의되지 않은 catch되지 않은 ReferenceError가가"

var bounds = new google.maps.LatLngBounds(swLatLn, neLatLn); 

그러나 나는 콘솔이 오류를 반환하기 때문에, 실패했습니다.

감사합니다.

+0

나쁜 아침을 가진 사람이 분명히 아래로 투표했습니다 .-- 나는 지오 코딩 및 역 지오 코딩을 수행 할 수있는 많은 연구를 수행했습니다. 실제로 위의 코드 대부분은 SA에 대해 꽤 많은 투표를 한 대답에서 비롯된 것입니다. 그래서 투표를 내 질문에 말이되지 않습니다. 다른 아마추어 (!)에게도 도움이 될 수 있습니다. 특히 나는 SA에 관한 비슷한 질문을 보지 못했다. –

답변

1

results[1].geometry.bounds.Ca.k; 등을 사용하지 마십시오. 이들은 변경 될 수있는 서류 미비 된 재산입니다.

대신 documented LatLngBounds methodsgetNorthEast()getSouthWest()을 사용하여 LatLng을 반환합니다. 당신이 위도와 경도 값을해야하는 경우

당신은 그 (것)은 lat()lng() 방법으로 얻을 수 있습니다 :이 도움이

var lat = results[1].geometry.bounds.getSouthWest().lat(); 
var lng = results[1].geometry.bounds.getSouthWest().lng(); 
// And so on... 

희망을.

+0

이것은 확실히 도움이되었습니다. 그 점에 대해 감사드립니다. 매일 나는 조금 더 배우고있다. –

관련 문제