JSON 结果到 Google 地图上的标记和信息框

发布于 2024-11-09 05:27:23 字数 1295 浏览 0 评论 0原文

JSON 结果:

[{"Company":"Company1","Country":"US","Phone":"209-555-8400","Website":"www.mywebsite.com","Latitude":35.782,"Longitude":-120.2269},
{"Company":"Company2","Country":"US","Phone":"909-555-5500","Website":"www.mywebsite.com","Latitude":36.112782,"Longitude":-111.52691},
{"Company":"Company3","Country":"US","Phone":"702-555-5200","Website":"www.mywebsite.com","Latitude":37.0427,"Longitude":-112.1818},
{"Company":"Company4","Country":"US","Phone":"602-555-5600","Website":"www.mywebsite.com","Latitude":38.4369,"Longitude":-113.8671},
{"Company":"Company5","Country":"US","Phone":"800-555-4716","Website":"www.mywebsite.com","Latitude":39.244946,"Longitude":-114.211941}]

视图中的 Javascript

        var locations = ["How can I get my JSON results in here"]

        var map = new google.maps.Map(document.getElementById('map_canvas'), {
            zoom: 2,
            center: new google.maps.LatLng(40.0, -35.0),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var infowindow = new google.maps.InfoWindow();

        var marker, i;

        for (i = 0; i 

我需要这样的东西:

var locations = ['Company1 209-555-8400 US www.mywebsite.com', 35.78, -120.23]

JSON results:

[{"Company":"Company1","Country":"US","Phone":"209-555-8400","Website":"www.mywebsite.com","Latitude":35.782,"Longitude":-120.2269},
{"Company":"Company2","Country":"US","Phone":"909-555-5500","Website":"www.mywebsite.com","Latitude":36.112782,"Longitude":-111.52691},
{"Company":"Company3","Country":"US","Phone":"702-555-5200","Website":"www.mywebsite.com","Latitude":37.0427,"Longitude":-112.1818},
{"Company":"Company4","Country":"US","Phone":"602-555-5600","Website":"www.mywebsite.com","Latitude":38.4369,"Longitude":-113.8671},
{"Company":"Company5","Country":"US","Phone":"800-555-4716","Website":"www.mywebsite.com","Latitude":39.244946,"Longitude":-114.211941}]

Javascript in View


        var locations = ["How can I get my JSON results in here"]

        var map = new google.maps.Map(document.getElementById('map_canvas'), {
            zoom: 2,
            center: new google.maps.LatLng(40.0, -35.0),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var infowindow = new google.maps.InfoWindow();

        var marker, i;

        for (i = 0; i 

I need something like this:

var locations = ['Company1 209-555-8400 US www.mywebsite.com', 35.78, -120.23]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初雪 2024-11-16 05:27:23

看一下将 json 解析为对象。像 JSON.net 这样的东西会很有用。

Have a look at parsing the json into an object. Something like JSON.net would be useful.

今天小雨转甜 2024-11-16 05:27:23

您可以使用 jquery 解析器,

var dataJson = $.parseJSON(data);

然后循环遍历数组

for(var i=0;i<dataJson.length;i++)
{
   var company = dataJson[i].Company;
   ....
}

,或者如果您想要上面准备好的字符串,您可以在服务器端

Type myType = myObject.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
String.Join(" ",  props.select(p => p.GetValue(yourObject,null)))

以简洁的方式设置它

String.Join(" ",  (List<PropertyInfo>)(object.GetType().GetProperties()).select(p => p.GetValue(yourObject,null)))

you can use jquery parser

var dataJson = $.parseJSON(data);

then you loop through the array

for(var i=0;i<dataJson.length;i++)
{
   var company = dataJson[i].Company;
   ....
}

alternatively if you want the ready string above, you can set it in server side

Type myType = myObject.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
String.Join(" ",  props.select(p => p.GetValue(yourObject,null)))

in concise way

String.Join(" ",  (List<PropertyInfo>)(object.GetType().GetProperties()).select(p => p.GetValue(yourObject,null)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文