在Java Maps API中使用JSON标记和Javascript [英] Using JSON markers in Google Maps API with Javascript

查看:108
本文介绍了在Java Maps API中使用JSON标记和Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程很陌生,所以我有点不敢发表.但是我已经做了两天,对于大多数人来说可能很简单,我想我会在这里尝试...

I am quite new to programming, so I'm a little scared to post. But I've been working on something that's likely quite simple for most of you for two days and thought I'd try here...

我正在尝试在Google Maps API中创建标记.

I am trying to create markers in the Google Maps API.

如果我像这样将数据硬编码到javascript中,它的工作原理就很吸引人.

If I hard code the data into javascript, like this, it works like a charm.

var locations = [
['1 Bedroom House', 55.1111, -100.1111, 1, 'This house has 1 bedroom.'],
['2 Bedroom House', 56.1111, -100.1111, 2, 'This house has 2 bedrooms.'],
['3 Bedroom House', 57.1111, -100.1111, 3, 'This house has 3 bedrooms.'],
['4 Bedroom House', 58.1111, -100.1111, 4, 'This house has 4 bedrooms.']
];

我有一个JSON请求,需要从那里获取标记数据.

I have a JSON request and need to get the marker data from there instead.

在您说出来之前,我确定我要离开这里……但这就是我得到的:

Before you say it, I'm sure I'm WAY off on here... but this is what I got:

var locations = new Array();

$.getJSON("[URL]", function(json) {
        for(var i = 0; i < json.houses.length; i++) {
            var name = json.houses[i].house.name;
            var markerlat = json.houses[i].house.lat;
            var markerlng = json.houses[i].house.lng;
            var description = json.houses[i].house.description;

locations[i] = new Array();
locations[i].push('\'' + name + '\', ' + markerlat + ', ' + markerlng + ', ' + i + ', ' + '\'' + description + '\'');

}
});

谢谢!

推荐答案

通过查看代码的这一部分,我想知道如果没有单独的locations数组是否可以解决它.

By looking at that part of your code, I wonder if it could be solved without the separate locations array.

如果您使用以下类型的代码在$ .getJSON函数的成功处理程序中扩展 for 循环,是否可行?

Could it work if you extend the for loop in the success handler of the $.getJSON function with following kind of code:

new google.maps.Marker({
    position: new google.maps.LatLng(markerlat, markerlng),
    title: name,
    map: [id of the map canvas element]
    });

如果还希望提供描述,则必须将侦听器附加到触发信息窗口的标记上.有关信息窗口叠加层及其用法的更多详细信息,请访问 Google Maps API文档站点.

If you want to have the descriptions also available, you'll have to attach listeners to the markers that trigger info windows. More details of the Info Window overlays and their usage can be found from Google Maps API documentation site.

这篇关于在Java Maps API中使用JSON标记和Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆