如何使用谷歌地图的地理coder.getLatLng()和它的结果存储在数据库中? [英] How do I use Google Maps geocoder.getLatLng() and store its result in a database?

查看:283
本文介绍了如何使用谷歌地图的地理coder.getLatLng()和它的结果存储在数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!即时试图使用getLatLng()进行地理code邮政/邮区codeS的列表,并在数据库中存储所生成的点后面将要放置在地图上。这是我到目前为止有:

Hey everybody! Im trying to use getLatLng() to geocode a list of postal/zip codes and store the generated point in the database to be placed on a map later. This is what I've got so far:

 $(".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?");
    });
}

但我发现 this.lat是不是在我的错误控制台功能当我尝试序列化点对象或使用$。员额()

but I'm getting this.lat is not a function in my error console when i try to serialize the point object or use it in $.post()

从我的研究,我理解地理coder.getLatLng()是异步的,怎么会影响我想要做什么?我没有运行在一个循环这个code,我试图用匿名的回调函数来邮点。

From my research, I understand that geocoder.getLatLng() is asynchronous, how would that affect what I'm trying to do? I'm not running this code in a loop, and I'm trying to post the point using the anonymous callback function.

我怎样才能挽救信息点后使用?

How can I save the information from point to use later?

更新

创建标记,并尝试后仍然导致 this.lat是不是在错误控制台功能

Creating a marker and trying to post that still results in the this.lat is not a function in the error console.

$(".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?");
                });
            }
        });

    });

**另一个更新**

** Another Update **

我真的需要拯救地球codeD地址后,即使我存储的纬度/经度值在我的数据库和重拍标记,当我准备把它放到地图。同样,序列化或张贴 - 看似使用比谷歌地图功能的其他任何方式点给出的 this.lat不是一个函数例外在我的错误日志。

I really need to save the geocoded address for later, even if I store the latitude/longitude values in my database and remake the marker when I'm ready to put it onto a map. Again, serializing or posting - seemingly using the point in any way other than in google maps functions gives the this.lat is not a function exception in my error log.

我使用asp.net的MVC - 是否有任何的框架存在,将使这更容易?我真的需要这方面的帮助。谢谢你。

I'm using asp.net mvc - are there any frameworks out there that would make this easier? I really need help with this. Thanks.

推荐答案

如果您坚持2天,也许一个新的V3开始将是一个很好的事情,这个文档片断做​​了similair工作对我来说...

If your stuck for 2 days maybe a fresh v3 start would be a good thing, this snipped does a similair job for me...

          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);


            });
    }

这篇关于如何使用谷歌地图的地理coder.getLatLng()和它的结果存储在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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