2012-12-22 5 views
-1
Google지도 API는 국가 이름과 도시를 돌아 만들고, 변수 장고에 저장하는 방법

역 지오 코딩 + Google지도 API + 장고

+0

pymaps를 사용하고 있습니까? 값을 절약하는 것이 장고와 어떤 관련이 있습니까? Google지도, django 및 저장 데이터는 모두 3 가지 뚜렷한 개념입니다. 자신이하고있는 일을 분명히 밝히고 문제가있는 경우. –

답변

1

귀하의 질문에이 정보 부족으로, 아래의 구글의 자동 완성 텍스트 입력 . 위치를 입력하면 li 태그 내에 표시된 도시, 주, 국가, 우편 번호, 위도, 경도 (찾은 곳)가 반환됩니다. 이 방법을 사용하여 li 대신 다른 변수에 저장할 수 있습니다. Here은 그 중 하나입니다.

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true"></script> 
<script> 
    $(document).ready(function(){ 
     var geocoder; 
     geocoder = new google.maps.Geocoder(); 
     var defaultBounds = new google.maps.LatLngBounds(
      new google.maps.LatLng(-33.8902, 151.1759), 
      new google.maps.LatLng(-33.8474, 151.2631)); 

     var input = document.getElementById('location'); 
     var options = { 
      bounds: defaultBounds, 
      types: ['(regions)'] 
     }; 

     autocomplete = new google.maps.places.Autocomplete(input, options); 

     google.maps.event.addListener(autocomplete, 'place_changed', function() { 
      var place = autocomplete.getPlace(); 
      var city; 
      var state; 
      var country; 
      var postcode; 
      var latitude; 
      var longitude; 

      if (place){ 
       address_components = place.address_components; 
       if (address_components){ 
        for (index in address_components){ 
         component = address_components[index]; 
         if (component.types.indexOf('locality') > -1){ 
          city = component.long_name; 
         } 
         if (component.types.indexOf('administrative_area_level_1') > -1){ 
          state = component.long_name; 
          if (!state){ 
           if (component.types.indexOf('administrative_area_level_2') > -1){ 
            state = component.long_name; 
            if (!state){ 
             if (component.types.indexOf('administrative_area_level_3') > -1){ 
              state = component.long_name; 
             } 
            } 
           } 
          } 
         } 
         if (component.types.indexOf('country') > -1){ 
          country = component.long_name; 
         } 
         if (component.types.indexOf('postal_code') > -1){ 
          postcode = component.long_name; 
         } 
        } 
       } 
       if (place.geometry){ 
        if (place.geometry.location){ 
         latitude = place.geometry.location.lat(); 
         longitude = place.geometry.location.lng(); 
        } 
       } 
      } 
      if (city){ 
       $('.city').text('City: ' + city); 
      }else{$('.city').text('City: ');} 
      if (state){ 
       $('.state').text('State: ' + state); 
      }else{$('.state').text('State: ');} 
      if (country){ 
       $('.country').text('Country: ' + country); 
      }else{$('.country').text('Country: ');} 
      if (postcode){ 
       $('.postcode').text('Postcode: ' + postcode); 
      }else{$('.postcode').text('Postcode: ');} 
      if (latitude){ 
       $('.latitude').text('Latitude: ' + latitude); 
      }else{$('.latitude').text('Latitude: ');} 
      if (longitude){ 
       $('.longitude').text('Longitude: ' + longitude); 
      }else{$('.longitude').text('Longitude: ');} 
     }); 
    }); 
</script> 
</head> 
<body> 
<input type="text" id="location" name="location"> 
<ul> 
    <li class="city">City: </li> 
    <li class="state">State: </li> 
    <li class="country">Country: </li> 
    <li class="postcode">Postcode: </li> 
    <li class="latitude">Latitude: </li> 
    <li class="longitude">Longitude: </li> 
</ul> 
</body> 
</html>