【问题标题】:keep geography coords from ajax避免ajax的地理坐标
【发布时间】:2018-03-23 17:00:48
【问题描述】:

晚上好, j 尝试从用户那里检索数据,但问题是由于“,”经度和纬度,我无法处理坐标,我不知道如何处理这些给定,我尝试使用 parsefloat 但没有结果,谢谢 代码控制器动作:

    public ActionResult GetNeaarByLocations(string CurrentLat, string CurrentLng)
    {
        using (GeolocationTestEntities context = new GeolocationTestEntities ())
        {
            var CurrentLocation = DbGeography.FromText("POINT(" + CurrentLat + " " + CurrentLng + ")");
            //var CurrentLocation = DbGeography.FromText("POINT(36,806494799999996 10,181531600000001)");
            var places = (from u in context.schoolinfo orderby u.Location.Distance(CurrentLocation)
                          select u).Take(4).Select(x=>new schoollinfo(){ Name = x.name ,Lat = x.Location.Latitude, Lng = x.Location.Longitude,Distance = x.Location.Distance(CurrentLocation)});
            var nearschools = places.ToList();
            return Json(nearschools , JsonRequestBehavior.AllowGet);
        }
    }

这是代码 Ajax:

jQuery.ajax({
                    cache: false,
                    type: "POST",
                    url: "@Url.Action("GetNeaarByLocations")",
                    dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({ CurrentLng:currentLatLng.longitude, CurrentLat: currentLatLng.latitude }),
                success: function (data) {
                    if (data != undefined) {
                        $.each(data, function (i, item) {
                            addMarker(item["lat"], item["lng"], "Click to get  directions");

                        })
                    }
                },
                failure: function (errMsg) {
                    alert(errMsg);
                }

            });

谢谢大家。

【问题讨论】:

    标签: c# json ajax asp.net-mvc-5 asp.net-ajax


    【解决方案1】:

    我们需要将 lng & lant 转换为 double

       var currentLatLng = position.coords;
                    var Lat = currentLatLng.latitude;
                    var Lng = currentLatLng.longitude;
                    var LatDouble = parseFloat(Lat);
                    var LngDouble = parseFloat(Lng);
    

    对于 Ajax 来说就是删除 att stringify

     data: JSON({ CurrentLng:LngDouble , CurrentLat: LatDouble  }),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 2016-07-19
      • 2020-08-24
      • 2013-09-09
      相关资源
      最近更新 更多