邮政编码检查问题 - 我哪里错了?

时间:2016-06-27 15:11:29

标签: javascript

这个想法是申请人输入他的邮政编码,如果他的区域在距他所在地10英里的范围内,他就被送到一页。

如果不是,他被指向另一个人。

我遇到的问题是搜索框出现在网页上,但是当选择搜索时,没有任何反应。

HTML:

// API Key
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=AIzaSyBC8nlMS-JbKNcFqF8mTO_cnT5OFqV94Xw&libraries=geometry=false"></Script>

<div class="map">
  <h4>Apply for funding</h4>
  <Br>
  <p>To find out if you are eligible for funding please enter your postcode into the search.</p>
  <div class="search">
  <Br>
    <p>Postcode search</p>
    <input type="text" value="Enter postcode" class="searchField location" title="Apply for funding search">
    <input type="text" value="boosh" style="display:none">
    <input value="search" class="searchButton locationSearch" type="button" title="Apply for funding search">

        </div>
    </div>
  //Above appears to give me search box but if I do not enter the first script line, nothing shows on website
 // Postcode entered but nothing happens

我的JavaScript:

<script type="text/javascript">

// locations where eligible

var locations = [{"Address":"Oxfordshire ERF","Lat":"51.925128","Lng":"-1.215956","PostCode":"ox27 7ph"},{"Address":"Thames","Lat":"51.383865","Lng":"-0.138555","PostCode":"cr0 4td"},{"Address":"Devon","Lat":"50.915447","Lng":"-3.332914","PostCode":"ex15 3ep"},{"Address":"Somerset (D)","Lat":"51.080808","Lng":"-2.553969","PostCode":"ba7 7nr"},
{"Address":"Somerset (W)","Lat":"51.18585","Lng":"-2.986303","PostCode":"ta6 4tf"},{"Address":"Derbyshire","Lat":"53.252614","Lng":"-1.328572","PostCode":"s44 5hs"},
{"Address":"Suffolk (M)","Lat":"52.108111","Lng":"1.087697","PostCode":"ip6 0nw"},{"Address":"Cornwall","Lat":"50.425197","Lng":"-4.439917","PostCode":"pl14 3qd"},
{"Address":"Manchester (P)","Lat":"53.579453","Lng":"-2.265505","PostCode":"bl9 8qz"},{"Address":"Kent","Lat":"51.299362","Lng":"1.101955","PostCode":"ct2 0pu"},
{"Address":"Hampshire","Lat":"50.991236","Lng":"-1.534517","PostCode":"so51 6ga"},{"Address":"Dorset","Lat":"50.705269","Lng":"-2.15095","PostCode":"bh20 7pa"},
{"Address":"Wiltshire","Lat":"51.435258","Lng":"-1.981372","PostCode":"sn11 8tf"},{"Address":"Runcorn (ERF)","Lat":"53.331395","Lng":"-2.756128","PostCode":"wa7 4hg"},
{"Address":"Exeter (ERF)","Lat":"50.704251","Lng":"-3.522272","PostCode":"ex2 8qe"},{"Address":"Bolton (ERF)","Lat":"53.566933","Lng":"-2.408110","PostCode":"bl3 2np"},
{"Address":"Cardiff (ERF)","Lat":"51.470867","Lng":"-3.151157","PostCode":"cf24 5en"},{"Address":"Peterborough (ERF)","Lat":"52.575185","Lng":"-0.206181","PostCode":"pe1 5ur"}];

$(document).ready(function() {

  $(".searchField.location").focus(function() {
     if($(this).val() == "Enter postcode")
     {
       $(this).val(""); 
     }
  });
  $(".searchField.location").blur(function() {
     if($(this).val() == "")
     {
        $(this).val("Enter postcode");
     }
  });

  $(".locationSearch").click(function(e) {
    e.preventDefault();
    var location = $(".searchField.location").val();
    var geo = new google.maps.Geocoder;
    var currentLocation = geo.geocode({'address':location}, function(results, status) ''
      if(status == google.maps.GeocoderStatus.OK) {
        var found = false;
        $.each(locations, function() {
          var lat = parseFloat(this.Lat);
          var lng = parseFloat(this.Lng);
          var storedLocation = new google.maps.LatLng(lat, lng);
          var distance = google.maps.geometry.spherical.computeDistanceBetween(storedLocation, results[0].geometry.location);
          var distance = distance * 0.000621371192237334;
          if(distance <= 10)
          {
            found = true;
          }
        });
        if(found)

// If eligible go to this page

          window.location.href ="/Funding/Eligible for funding/?postcode=" + location;

        }else

// If no then this page
          window.location.href ="/Funding/Not eligible for funding/?postcode=" + location;

    }
  });

  </script>

0 个答案:

没有答案