Guest User

Untitled

a guest
Sep 23rd, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
  4.     <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
  5. </head>
  6. <body>
  7.     <span><input type="checkbox" name="other_address" value="1"> I want to change address</span>
  8.     <input type="text" name="input_location" value="Bismarckallee 3, Freiburg" id="input_location" placeholder="Address (Street, City)">
  9.     <input type="hidden" name="input_coordinates" value="47.997337,7.841653">
  10.     <div id="map_canvas" class="user_location_map" style="width: 100%; height: 300px;"></div>
  11.  
  12.     <script type="text/javascript">
  13.         var markersArray = [];
  14.  
  15.         function addMarker(location) {
  16.             marker = new google.maps.Marker({
  17.                 position: location,
  18.                 map: map
  19.             });
  20.             markersArray.push(marker);
  21.         }
  22.  
  23.         function deleteOverlays() {
  24.             if (markersArray) {
  25.                 for (i in markersArray) {
  26.                     markersArray[i].setMap(null);
  27.                 }
  28.                 markersArray.length = 0;
  29.             }
  30.         }
  31.  
  32.         function setDomCoords(x, y)
  33.         {
  34.             $('input[name=input_coordinates]').val( x + ',' + y );
  35.         }
  36.  
  37.         function getDomCoords()
  38.         {
  39.             var coords = $('input[name=input_coordinates]').val().split(',');
  40.             return new google.maps.LatLng(coords[0], coords[1]);
  41.         }
  42.  
  43.         function gotoCoords(coords)
  44.         {
  45.             map.setCenter(coords);
  46.             map.setZoom(14);
  47.             addMarker(coords);
  48.         }
  49.  
  50.         function gotoAddress(address)
  51.         {
  52.             var geocoder = new google.maps.Geocoder();
  53.             geocoder.geocode({
  54.                 address : address,
  55.                 region: 'no'
  56.             }, function(results, status) {
  57.                 if (status.toLowerCase() == 'ok') {
  58.                     var coords = new google.maps.LatLng(
  59.                         results[0]['geometry']['location'].lat(),
  60.                         results[0]['geometry']['location'].lng()
  61.                     );
  62.  
  63.                     gotoCoords(coords);
  64.                     setDomCoords(coords.lat(), coords.lng());
  65.                 }
  66.             });
  67.         }
  68.  
  69.         var map = new google.maps.Map( document.getElementById("map_canvas"),  {
  70.             mapTypeId: google.maps.MapTypeId.ROADMAP,
  71.             panControl: false,
  72.             streetViewControl: false,
  73.             mapTypeControl: false
  74.         });
  75.  
  76.         var temp_address = $('input[name=input_location]').val();
  77.         var temp_coords = $('input[name=input_coordinates]').val();
  78.  
  79.         gotoCoords(getDomCoords());
  80.  
  81.         $('input[name=other_address]').change(function() {
  82.             if($('input[name=input_location]').val()){
  83.                 $('input[name=input_location]').val('');
  84.                 $('input[name=input_coordinates]').val('');
  85.             }else{
  86.                 $('input[name=input_location]').val(temp_address);
  87.                 $('input[name=input_coordinates]').val(temp_coords);
  88.             }
  89.         });
  90.  
  91.         $('input[name=other_address]').change(function(){
  92.             if($('input[name=other_address]').is(':checked')){
  93.                 deleteOverlays();
  94.                 map.setZoom(5);
  95.  
  96.                 if($('input[name=input_location]').val()){
  97.                     $('input[name=input_location]').val('');
  98.                     $('input[name=input_coordinates]').val('');
  99.                 }
  100.             } else {
  101.                 $('input[name=input_location]').val(temp_address);
  102.                 $('input[name=input_coordinates]').val(temp_coords);
  103.             }
  104.         });
  105.  
  106.         $('input[name=input_location]').change(function(){
  107.             gotoAddress($(this).val());
  108.         });
  109.     </script>
  110. </body>
  111. </html>
Advertisement
Add Comment
Please, Sign In to add comment