在ASP.NET中从Google Maps API Version3上的代码绑定经度和纬度

塞尔瓦

源代码aspx页面:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
    function loadData(e) {
        e.preventDefault();
        var ltlng = [];
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Default.aspx/getData",
            dataType: "json",
            success: function (data) {
                $(data.d).each(function (index, item) {
                    var firstValue = parseFloat(item.FirstValue).toFixed(2);
                    var secondValue = parseFloat(item.SecondValue).toFixed(2);
                    ltlng.push(new google.maps.LatLng(firstValue, secondValue));
                });
            },
            error: function () {
                alert("Error");
            }
        });
        //alert("Test");
        markicons(ltlng);
    }

    var map; var infowindow;
    function InitializeMap() {
        var latlng = new google.maps.LatLng(40.756, -73.986);
        var myOptions =
    {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
        map = new google.maps.Map(document.getElementById("map"), myOptions);
    }

    function markicons(ltlng) {
        InitializeMap();
        map.setCenter(ltlng[0]);
        for (var i = 0; i <= ltlng.length; i++) {
            if (ltlng[i] != undefined) {
                marker = new google.maps.Marker({
                    map: map,
                    position: ltlng[i]
                });
                alert(ltlng[i]);
                (function (i, marker) {
                    google.maps.event.addListener(marker, 'click', function () {
                        if (!infowindow) {
                            infowindow = new google.maps.InfoWindow();
                        }
                        infowindow.setContent("Message" + i);
                        infowindow.open(map, marker);
                    });
                })(i, marker);

            }
            else {
            }
        }
    }
    window.onload = loadData;
</script>
<div id="map" style="width: 800px; height: 500px;">
</div>

C#的源代码:

    [WebMethod]
    public static object getData()
    {
        var obj = new { FirstValue = 17.22, SecondValue = 78.28 };
        var objList = (new[] { obj }).ToList();
        objList.Add(new { FirstValue = 13.5, SecondValue = 79.2 });
        objList.Add(new { FirstValue = 15.24, SecondValue = 77.16 });
        return objList;
    }

我得到这样的结果, 在此处输入图片说明

经过测试的浏览器:谷歌浏览器。请建议我如何解决此错误。

马丁·迪克森(Martin Dixon)

您正在呼叫您的markicons(ltlng); 初始化对数据的ajax请求后的方法。

取回数据后,需要调用此方法。

例子:

success: function (data) {
            $(data.d).each(function (index, item) {
                var firstValue = parseFloat(item.FirstValue).toFixed(2);
                var secondValue = parseFloat(item.SecondValue).toFixed(2);
                ltlng.push(new google.maps.LatLng(firstValue, secondValue));
            });

            // Call method here.
            markicons(ltlng);
        },

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google Maps API 返回空的纬度和经度

在Google Maps API中使用经度和纬度变量

解析 Google Maps API 的纬度和经度 (Bash)

来自Google Maps Api JSON的经度和纬度?

根据纬度和经度重点关注Google Maps API

Android上的Google Maps API不接受纬度和经度值吗?

代码点火器-无法在Google Maps V3 API中检测到我当前的纬度/经度位置

使用Google Maps API在Android中查找设备当前位置的纬度和经度值

ASP和Google Map Api:通过google.maps.event.addListener显示asp:textbox的位置

Github中的Google Maps Api关键代码

Google Maps API:分别获取经度/纬度

Google Maps API v3-将Google Maps Circle从搜索框中放置为纬度/经度

是否可以从google maps API获取过去旅行过的地方的经度,纬度和时间戳?

通过Google Maps API使用地址而不是经度和纬度

如何从Google Maps Places api对象获取经度和纬度?

检查纬度和经度的公式在没有Google Maps Api的区域内

Google Maps API向信息窗口添加标记的纬度和经度

地理位置:使用纬度和经度填写表格。Google Maps API

来自 SQL 的 Asp.Net Core MVC Google Maps api Plot Ployline

如何使用 PHP JSON 对象从 Google Maps API 获取 5 个附近地点的纬度和经度?

Apple Maps 和 Google Maps API 可行性

Google Maps API和Google Maps位置在PHP中显示了不同的位置

Google ASP中的Google Maps路线

通过C#webforms asp.net中的google map api从下拉列表中的选定城市中获取textbox1和textbox2的纬度和经度

延迟在Google Maps API V3中添加图层

Google Maps Javascript API V3中的旋转标记

在Google Maps API上重新加载标记

在Google Maps API Android上绘画

更新Google Maps上的位置-API