Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于在Google地图中放置标记的JSON数据_C#_Json_Google Maps - Fatal编程技术网

C# 用于在Google地图中放置标记的JSON数据

C# 用于在Google地图中放置标记的JSON数据,c#,json,google-maps,C#,Json,Google Maps,我试图根据JSON中的坐标放置标记,但我认为我做得不对 public class Stadiums { public int ID { get; set; } public string Team { get; set; } public decimal Latitude { get; set; } public decimal Longitude { get; set; } } public void GetStadiums() {

我试图根据JSON中的坐标放置标记,但我认为我做得不对

    public class Stadiums
{
    public int ID { get; set; }

    public string Team { get; set; }

    public decimal Latitude { get; set; }

    public decimal Longitude { get; set; }
}


public void GetStadiums()
    {
        var con = ConfigurationManager.ConnectionStrings["NFLConnectionString"].ToString();

        List<Stadiums> matchingStadiums = new List<Stadiums>();
        using (SqlConnection myConnection = new SqlConnection(con))
        {

            string oString = "Select * from Stadiums";
            SqlCommand oCmd = new SqlCommand(oString, myConnection);
            myConnection.Open();
            using (SqlDataReader oReader = oCmd.ExecuteReader())
            {
                while (oReader.Read())
                {
                    matchingStadiums.Add(new Stadiums
                    {
                        Team = oReader["Team"].ToString(),
                        ID = Convert.ToInt32(oReader["ID"]),
                        Latitude = Convert.ToDecimal(oReader["Latitude"]),
                        Longitude = Convert.ToDecimal(oReader["Longitude"])
                    });
                }

                ResultstoJSON(matchingStadiums);

                myConnection.Close();
            }
        }
    }
    public void ResultstoJSON(List<Stadiums> stadiums)
    {
        var jsonSerialiser = new JavaScriptSerializer();
        var json = jsonSerialiser.Serialize(stadiums);
        this.JSONData = json;
    }
我已经验证了JSONData包含我正在寻找的JSON格式的所有数据。从这里开始,我不知道如何在谷歌地图中获取数据和放置PIN

@{
Layout = null;
}

<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
    html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px;
    }
</style>

<script src="https://maps.googleapis.com/maps/api/js?     v=3.exp&signed_in=true"></script>
<script>
    var myLatlng = new google.maps.LatLng(37.09024, -95.712891);

    function initialize() {
        var mapOptions = {
            zoom: 5,
            center: new google.maps.LatLng(37.09024, -95.712891)
        };
       var map = new google.maps.Map(document.getElementById('map-canvas'),
            mapOptions);

        var marker = new google.maps.Marker({
            position: (myLatlng),
            map: map,
            title: 'Hello World!'
        });
    }

    google.maps.event.addDomListener(window, 'load', initialize);

</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
链接到JSON数据:


再次澄清一下,我试图根据JSON数据中具有坐标的项目在Google Maps中放置PIN/标记。

查看您提供的内容和您的JSON

您的控制器将需要将json标记数据返回到客户机-目前您没有返回任何内容

在客户端中,一旦获得json,请将其解析为对象:

var markerData = JSON.parse(json);
然后在创建地图后,通过markerData数组中的每个项目循环,并根据其纬度和纵向值为其创建标记:

$.each(markerData,function()
{
     var myLatlng = new google.maps.LatLng(this.Latitude, this.Longitude);

       var marker = new google.maps.Marker({
        position: (myLatlng),
         data:this,
        map: map,
        title: 'Hello World!'
    });                
}
这里有一个JS fildle可以帮助你


请提供更多信息,包括指向JSON数据的链接或字符串变量中包含JSON的JSFIDLE,我会为您提供帮助。除了我做得不对之外,您还有什么具体问题吗。我在帖子的底部添加了一个pastebin链接。我的问题是,如何根据JSON数据向google maps正确添加PIN。你能解释一下让我的控制器将JSON标记数据返回给客户端是什么意思吗?你的标记数据来自服务器,对吗?因此,当将其返回到客户端浏览器时,您将返回json字符串。上面的代码没有显示如何将json返回到浏览器。如果浏览器中有json数据,那么您可以按照上面我向您展示的步骤进行操作