检索全局变量

时间:2014-11-30 07:21:37

标签: javascript geocoding

如何将city值检索到location对象?

var city = "";
var state = "";
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geocoder.geocode({
  'latLng': latlng
}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    //Check result 0
    var result = results[0];
    //look for locality tag and administrative_area_level_1
    for (var i = 0, len = result.address_components.length; i < len; i++) {
      var ac = result.address_components[i];
      if (ac.types.indexOf("locality") >= 0) city = ac.long_name;
      if (ac.types.indexOf("administrative_area_level_1") >= 0) state = ac.long_name;
    }
  }
});

var location = {lat: position.coords.latitude, lng: position.coords.longitude, city: city};

2 个答案:

答案 0 :(得分:1)

geocoder.geocode的调用是异步的。在异步代码返回之前,代码执行会获得var location的声明;因此城市尚未定义。

提前声明位置,然后在回调函数中添加到位置对象

var location = {lat: position.coords.latitude, lng: position.coords.longitude};
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geocoder.geocode({
  'latLng': latlng
}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    //Check result 0
    var result = results[0];
    //look for locality tag and administrative_area_level_1
    for (var i = 0, len = result.address_components.length; i < len; i++) {
      var ac = result.address_components[i];
      if (ac.types.indexOf("locality") >= 0) location.city = ac.long_name;
      if (ac.types.indexOf("administrative_area_level_1") >= 0) location.state = ac.long_name;
    }
  }
});

答案 1 :(得分:1)

您可以提前声明location并在回调中使用它。像这样:

var city = "";
var state = "";
var location = {lat: position.coords.latitude, lng: position.coords.longitude}; // move `location` here
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geocoder.geocode({
  'latLng': latlng
}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    //Check result 0
    var result = results[0];
    //look for locality tag and administrative_area_level_1
    for (var i = 0, len = result.address_components.length; i < len; i++) {
      var ac = result.address_components[i];
      if (ac.types.indexOf("locality") >= 0) {
        city = ac.long_name;
        location.city = city; // and assign the `city` field here
      }
      if (ac.types.indexOf("administrative_area_level_1") >= 0) state = ac.long_name;
    }
  }
});

但是,这不是很实用,因为回调是异步的。如果将地理编码包装到函数中会更好。像这样:

function geocode(position, callback) {
    var city = "";
    var state = "";
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    geocoder.geocode({
      'latLng': latlng
    }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        //Check result 0
        var result = results[0];
        //look for locality tag and administrative_area_level_1
        for (var i = 0, len = result.address_components.length; i < len; i++) {
          var ac = result.address_components[i];
          if (ac.types.indexOf("locality") >= 0) {
            city = ac.long_name;
            callback({
              lat: position.coords.latitude, 
              lng: position.coords.longitude,
              city: city
            });
          }
          if (ac.types.indexOf("administrative_area_level_1") >= 0) state = ac.long_name;
        }
      }
    });
}

geocode(position, function(location) {
  // do whatever you would like to do with `location`.
})