Tuesday 15 June 2010

javascript - Changing the logic in my jquery script -


This is the script I'm working with:

Actually it works by geocoding When the submit button is pressed. However, if an option is selected from auto-listing link, the result will be geocode before the user clicks the button. In this case the script knows that the answer is already in it, and the result will not be geocode again.

Currently it works by storing result in this variable:

  lastQuery   

Checks:

  if (! Query || query === lastQuery)   

However, for logic, and simplifying my code For those who are contained in this code, I was thinking that it would be better to do the following:

  • Check whether the LNG when the submit button is killed, the latitude fields are empty, and if yes , So there is geographic deposits.
  • If the contents of the location field change, then remove coordinates from LNG latitude areas.

    Basically this works if the place is geographically and has been unchanged when it is presented, then we have the result and it does not need to be re geocode, But before deleting any changes in the location box, remove the geocode values, and so Geocoders know that we must remember the importance of the location when submitted.

    How do I integrate this argument, and besides, I'm stuck with my basic knowledge of JavaScript, I'm not sure that this is really a sensible solution.

    Can give you some ideas.

    There are some major differences:

    Turning on google.maps The Geocoder API in the Wired-based API

    (See also) Are good for

      // Rotate google.maps.Geocoder Promise API function googleGeocode (query) {var geocoder = new google.maps.Geocoder (), dfd = $. Idle (); Geocoder.geocode ({address: query}, function (result, status) {if (position === google.maps.GeocoderStatus.OK) {console.log ("googleGeocode", "success", query, result); dfd .resolve (query, result);} other {console.log ("googleGeocode", "failure", query); dfd.reject (query);}}); Return dfd.promise (); }   

    Then we can be it use the Geocode method:

      // If only query from the last time Has changed, so only geocode. Function geocode () {var geokeyword = $ .trim ($ ("# geokeyword") .val ()), loc = $ .trim ($ ("# loc"). Val ()), // Always use geokeyword Preferred place (if provided) query = geokeyword || Location, dfd; // Check for changes if (history.query === query) {console.log ("no change"); Dfd = $ Idle (); // Resolve with history already history dfd.resolve (history.query, history.results); Return dfd.promise (); } Return googleGeocode (query); }   

    use of a history object

    (one object is shown here, but your history can be an array or map of all previous geocode results). >

      var history = {lat: null, long: null, query: null, results: null},   

    which is requested for a geocode (above See)), and when a successful livecode is displayed (see below - success is a callback, which is given to callback then () , and the error callback is second ):

      function process (in history, submit) {// block only Submit Submit Submit == false auto-submission = (Wrong! Submit ==); Geocode () Then (function (query, result) {var form = $ ("# geocode_form"), lat = form.find ("# lat"), long = form.find ("# lng"), location = result [0] .geometry.location; console.log ("process location", "success", query, result); lat.val (location.lat ()); long.val (location.lng ()); history; query; query; History.results = results; history.lat = lat.val (); history.long = long.val (); if (submit) {console.log ("submit", lat.val (), long.val ()); Form.submit ();}}, function (query) {console.log ("process location", "failure", query);}); }    

No comments:

Post a Comment