helloxielan 2014-07-22 18:40 采纳率: 0%
浏览 31

将列表传递给Ajax

public List<double> GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";

            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();

            var cordinates = new List<double>();

            foreach (var result in googleResults.results)
            {

                cordinates.Add(result.geometry.location.lng);

                cordinates.Add(result.geometry.location.lat);

            }

            return cordinates;

        }

Now, I want two use the values in the list in my Ajax:

 $(function() {
                    $('#ajax').click(function() {
                        $.ajax({

                                url: '@Url.Action("GoogleGeoCode", "Home")',
                                context: document.body
                            })
                            .done(function(serverdata) {
                                data = serverdata;
                                $.each(data, function(i, item) {
                                    var marker = new google.maps.Marker({
                                        'position': new google.maps.LatLng(item[0], item.GeoLat[1]), <-- I thought Item would contain my list and I could use its index
                                        'map': map,
                                        'title': item.PlaceName,
                                        'Catch': item.Catch

                                    });
                                });
                            });
                    });

Im a beginner with Ajax i would really appreciate a step by step explanation on how it works if someone got the time. Thanks!

Updat: New class:

public class LongLatModel
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

Controller:

public LongLatModel GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";

            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();

            var cordinates = new List<double>();

            var longlat = new LongLatModel();


            foreach (var result in googleResults.results)
            {

                longlat.Latitude = result.geometry.location.lng;

                longlat.Longitude = result.geometry.location.lat;

            }

            return longlat;

        }

Ajax:

$(function() {
                    $('#ajax').click(function() {
                        $.ajax({
                                // in a real scenario, use data-attrs on the html
                                // rather than linking directly to the url
                                url: '@Url.Action("GoogleGeoCode", "Home")',
                                context: document.body
                            })
                            .done(function(serverdata) {
                                data = serverdata;
                                $.each(data, function(i, item) {
                                    var marker = new google.maps.Marker({
                                        'position': new google.maps.LatLng(data.longitude, data.latitude),
                                        'map': map,
                                        'title': item.PlaceName,
                                        'Catch': item.Catch

                                    });
                                });
                            });
                    });

EDIT:

Ok, so I managed to get the right info into the AJAX (i think..) However, I have trouble relising how to place these two values where they are supposed to be.

enter image description here

How can i loop throgh and place them in the right place?

$.each(data, function (i, item) {

var marker = new google.maps.Marker({ 'position': new google.maps.LatLng(LONG, LAT),

});

  • 写回答

1条回答 默认 最新

  • weixin_33724059 2014-07-22 18:50
    关注
    1. In the C# method, you should create a data strucutre for LatLog, instead of returning a List<double> in the method.

      class LatLongModel
      {
          public double Latitude {get;set;}
          public double Longitude{get;set;}
      }
      
    2. In JavaScript, in the done method, you can then check:

      data.Longitude; data.Latitude;
      
    3. You also should add fail in the Ajax request in case the request fails. Check out this snippet: http://jsfiddle.net/JhWt4/

    Update

    In the C# code you want to create and return a List

        public class GeoCodeResultModel
        {
            public double Latitude { get; set; }
            public double Longitude { get; set; }
            public string PlaceName { get; set; }
            public string Catch { get; set; }
        }
    
        public List<GeoCodeResultModel> GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
    
            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();
    
            var locations = new List<GeoCodeResultModel>();
    
            foreach (var result in googleResults.results)
            {
                locations.Add(new GeoCodeResultModel
                {
                     Longitude = result.geometry.location.lng,
                     Latitude = result.geometry.location.lat,
                     PlaceName = 'TODO',
                     Catch = 'TODO'
                }
            }
    
            return locations;
    
        }
    

    For the JavaScript Code:

    $.each(data, function(i, item) {
        var marker = new google.maps.Marker({
                     'position': new google.maps.LatLng(item.Latitude, item.Longitude), 
                     'map': map,
                     'title': item.PlaceName,
                     'Catch': item.Catch
        });
    });
    

    Disclaimer: This isn't tested code, it's to point you in the right direction.

    评论

    报告相同问题?

    悬赏问题

    • ¥80 关于海信电视聚好看安装应用的问题
    • ¥15 vue引入sdk后的回调问题
    • ¥15 求一个智能家居控制的代码
    • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
    • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
    • ¥20 求各位能用我能理解的话回答超级简单的一些问题
    • ¥15 yolov5双目识别输出坐标代码报错
    • ¥15 这个代码有什么语法错误
    • ¥15 给予STM32按键中断与串口通信
    • ¥15 使用QT实现can通信