如何使用Google Maps geocoder.getLatLng()并将其结果存储在数据库中?

Gal*_*len 11 javascript asp.net-mvc jquery google-maps google-maps-api-2

大家好!我试图使用getLatLng()对邮政/邮政编码列表进行地理编码,并将生成的点存储在数据库中以便稍后放置在地图上.这是我到目前为止所得到的:

 $(".geocodethis").click(function () {
    var geocoder = new GClientGeocoder();
    var postalCode = $(this).siblings(".postal").val();
    var id = $(this).siblings(".id").val();

    geocoder.getLatLng(postalCode, function (point) {
        if (!point) {
            alert(postalCode + " not found");
        } else {
            alert(point);
            var serializedPoint = $.param(point);                
            //Geocode(id, point);
        }
    });

});

function Geocode(id, point) {
    alert(point);
    $.post("/Demographic/Geocode/" + id, point, function () {
        alert("success?");
    });
}
Run Code Online (Sandbox Code Playgroud)

但是this.lat is not a function当我尝试序列化点对象或使用它时,我进入了我的错误控制台$.post()

根据我的研究,我明白这geocoder.getLatLng()是异步的,这会影响我正在尝试做什么?我没有在循环中运行此代码,我正在尝试使用匿名回调函数发布该点.

如何保存信息point以便以后使用?

更新

创建标记并尝试发布仍然会导致this.lat is not a function 错误控制台中的标记.

$(".geocodethis").click(function () {
        var geocoder = new GClientGeocoder();
        var postalCode = $(this).siblings(".postal").val();
        var id = $(this).siblings(".id").val();

        geocoder.getLatLng(postalCode, function (point) {
            if (!point) {
                alert(postalCode + " not found");
            } else {
                alert(point);
                var marker = new GMarker(point);

                $.post("/Demographic/Geocode/" + id, marker, function () {
                    alert("success?");
                });
            }
        });

    });
Run Code Online (Sandbox Code Playgroud)

**另一个更新**

我确实需要保存地理编码的地址以供日后使用,即使我将纬度/经度值存储在我的数据库中并在我准备好将其放入地图时重新制作标记.再次,序列化或发布 - 似乎在谷歌地图函数以外的任何方式使用该点this.lat is not a function在我的错误日志中给出了例外.

我正在使用asp.net mvc - 是否有任何框架可以使这更容易?我真的需要帮助.谢谢.

Arn*_*sss 11

如果你坚持2天可能是一个新的v3开始将是一件好事,这个snipped为我做了类似的工作......

          function GetLocation(address) {
          var geocoder = new google.maps.Geocoder();
          geocoder.geocode({ 'address': address }, function (results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                  ParseLocation(results[0].geometry.location);

              }
              else
                alert('error: ' + status);

          });
      }

  }

  function ParseLocation(location) {

      var lat = location.lat().toString().substr(0, 12);
      var lng = location.lng().toString().substr(0, 12);

      //use $.get to save the lat lng in the database
      $.get('MatchLatLang.ashx?action=setlatlong&lat=' + lat + '&lng=' + lng,
            function (data) {
                // fill textboss (feedback purposes only) 
                //with the found and saved lat lng values
                $('#tbxlat').val(lat);
                $('#tbxlng').val(lng);
                $('#spnstatus').text(data);


            });
    }
Run Code Online (Sandbox Code Playgroud)