从谷歌geocode jquery抓取国家 [英] Grabbing country from google geocode jquery

查看:111
本文介绍了从谷歌geocode jquery抓取国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个jquery脚本的帮助,它将通过Google地理编码系统对输入的位置进行地理编码。

I've had help to get a jquery script which will geocode a typed location via googles geocoding system.

我需要输入国家/地区的短名称领域。对于我的生活,我无法理解我应该如何整合,我所尝试的一切都不会回报价值。任何人都可以请提供一些见解,以实现这一目标的正确方式吗?

I need the short_name of country value put into the country field. For the life of me I can't get my head around how I should be integrating, and everything I try doesn't return the value. Can anyone please give some insight as to the correct way of achieving this please?

JSFIDDLE: http://jsfiddle.net/spadez/jCB4s/2/

JSFIDDLE: http://jsfiddle.net/spadez/jCB4s/2/

$(function () {
  var input = $("#loc"),
      lat   = $("#lat"),
      lng   = $("#lng"),
      lastQuery  = null,
      lastResult = null, // new!
      autocomplete;

  function processLocation(callback) { // accept a callback argument
    var query = $.trim(input.val()),
        geocoder;

    // if query is empty or the same as last time...
    if( !query || query == lastQuery ) {
      callback(lastResult); // send the same result as before
      return; // and stop here
    }

    lastQuery = query; // store for next time

    geocoder = new google.maps.Geocoder();
    geocoder.geocode({ address: query }, function(results, status) {
      if( status === google.maps.GeocoderStatus.OK ) {
        lat.val(results[0].geometry.location.lat());
        lng.val(results[0].geometry.location.lng());
        lastResult = true; // success!
      } else {
        alert("Sorry - We couldn't find this location. Please try an alternative");
        lastResult = false; // failure!
      }
      callback(lastResult); // send the result back
    });
  }

  autocomplete = new google.maps.places.Autocomplete(input[0], {
    types: ["geocode"],
    componentRestrictions: {
      country: "uk"
    }
  });

  google.maps.event.addListener(autocomplete, 'place_changed', processLocation);

  $('#searchform').on('submit', function (event) {
    var form = this;

    event.preventDefault(); // stop the submission

    processLocation(function (success) {
      if( success ) { // if the geocoding succeeded, submit the form
        form.submit()
      }
    });

  });
});

根据 API 我需要这个:

According to the API I need this:

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   types[]: string
 },
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}


推荐答案

解析搜索结果与国家/地区类型

Parse through the results looking for one with a type of "country"

示例那样会寻找一种类型为postal_code的结果

类似这样的东西应该可以工作(未测试):

Something like this should work (not tested):

geocoder.geocode({ address: query }, function(results, status) {
  if( status === google.maps.GeocoderStatus.OK ) {
    lat.val(results[0].geometry.location.lat());
    lng.val(results[0].geometry.location.lng());
    var address = results[0].address_components;
    for (var p = address.length-1; p >= 0; p--) {
      if (address[p].types.indexOf("country") != -1) {
      // do something useful with the country...
      // document.getElementById('postcode').innerHTML= address[p].long_name;
      }
    }
    lastResult = true; // success!
  } else {
    alert("Sorry - We couldn't find this location. Please try an alternative");
    lastResult = false; // failure!
  }
  callback(lastResult); // send the result back
});

这篇关于从谷歌geocode jquery抓取国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆