简体   繁体   English

Google Map Marker和嵌套的JSON以在Map Infowindow中显示数据

[英]Google Map Marker and nested JSON to show data in Map Infowindow

I am using Google Map to show some places and users in it via the info window. 我正在使用Google Map通过信息窗口在其中显示一些地点和用户。

Google Map Script: Google Map脚本:

<script>
var markers = [
['<p>New York</p> <p><a href="#myModal" role="button" class="btn" data-toggle="modal">More information</a></p>', 40.769090, -74.002740],
['<p>Washington, Finland</p> <p><a href="#myModal" role="button" class="btn" data-toggle="modal">More information</a></p>', 38.908127, -77.034863],
];

function initializeMaps() {
var myOptions = {
    zoom: 3,
    center: new google.maps.LatLng(40.769090, -74.002740),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
};
var image = 'img/logos.png';
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, i;
for (i = 0; i < markers.length; i++) {  
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(markers[i][1], markers[i][2]),
        map: map,
        icon: image
    });
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(markers[i][0]);
            infowindow.open(map, marker);
        }
    })(marker, i));
}}</script> 

The Script for Map Info window 地图信息脚本窗口

   <script> 
   var success = function(data) {
   $(".ID").append(data.sessions[0].ID); 
   $(".Users").append(data.sessions[0].Users); 
   $.each(data.sessions[0].Location, function(i, value){
   $(".Location").append('<li>' +  value.name + '</li>');
   })
   }
   $.getJSON("sample.json", success);
   </script>

The JSON file: JSON文件:

   {"sessions":[
    {
        "ID":"123",
        "Users":"4",
        "Location": 
            { "New York": [
                      { "id": 1, "name": "Mat" },
                      { "id": 2, "name": "Cat" }
                 ],
              "Washington": [
                      { "id": 1, "name": "Rat" },
                      { "id": 2, "name": "Sat" }
                 ]
            }   
    },
     {
        "ID":"456",
        "Users":"1",
        "Location": 
            { "New York": [
                      { "id": 1, "name": "Bat" }
                 ]
            }   
    }]}

The HTML: HTML:

    <div id="map_canvas"></div>   
    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">More Details</h3>
    </div>
    <div class="modal-body">
    <p class="ID">ID</p>
    <ul class="Location"></ul>
    </div>
    <div class="modal-footer">
     <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Save changes</button>
    </div>
    </div>

The problem I am facing is to show a specific user in its respective place. 我面临的问题是在特定位置显示特定用户。 For example when I click new york in the marker, it should show me the info window with two sessions and its details (according to the JSON file). 例如,当我在标记中单击“纽约”时,它应显示带有两个会话及其详细信息的信息窗口(根据JSON文件)。 I am not sure how to deal with marker ID and Nested JSON. 我不确定如何处理标记ID和嵌套的JSON。 Please help. 请帮忙。

There are a few problems in your yode you have to fix. 您的yode必须解决一些问题。

First: 第一:

 <script> 
   var success = function(data) {
   $(".ID").append(data.sessions[0].ID); 
   $(".Users").append(data.sessions[0].Users); 
   $.each(data.sessions[0].Location, function(i, value){
   $(".Location").append('<li>' +  value.name + '</li>');
   })
   }
   $.getJSON("sample.json", success);
   </script> 

That Script is loading when calling the page. 调用页面时正在加载该脚本。 So it puts the values for the modal in it at the beginning. 因此,它将模态的值放在开头。 So there are always the same information in it. 因此,其中始终包含相同的信息。

Good way should be to load the JSON File in the beginning and write it in a global variable. 好的方法应该是在开始时加载JSON文件并将其写入全局变量中。

When pushing the "More Information" button in your marker, you should call a javascript function with an id of the pushed city as parameter (not name, they aren't unique). 当按下标记中的“更多信息”按钮时,您应该调用一个以所推入的城市的ID作为参数的javascript函数(不是名称,它们不是唯一的)。

You have to put that id instead of the city name in your json. 您必须在json中输入该ID而不是城市名称。

Now the javascript function, which is called after pushing the "more information" button, should go in an iterative way (for each oder for) over the different sessions in your json and should look for an Location with the matching id. 现在,在按下“更多信息”按钮之后调用的javascript函数应该以迭代方式(针对每个odd)遍历json中的不同会话,并应查找具有匹配ID的Location。 When the id matches, append the data to the model. 当ID匹配时,将数据附加到模型。

The usage of nested JSON is really easy. 嵌套JSON的使用非常简单。 When you want to get the information of Mat in New york in the first session, you have to use the following code: 当您想在第一届会议上获得纽约的Mat信息时,您必须使用以下代码:

data.sessions[0].Location.NewYork[0] 

Have a look, that you better write the identifiers without a space. 看一下,最好将标识符写成没有空格。

So you can write a for construct like : 因此,您可以编写一个for结构,例如:

 for(var i = 0; i<data.sessions.length;i++){
       var locations = data.sessions[i].Location;
       if ('Washington' in locations) { 
          //Code to add it in your city information window
              console.log(locations.Washington);

       }
   }

the variable id is the passed parameter of the clicked marker. 变量ID是点击标记的传递参数。

You also should check your whole code, there are a few parts you should better change (The model won't really work with more than 1 Session) 您还应该检查您的整个代码,有一些部分需要更好地更改(该模型实际上不能在超过1个Session的情况下工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM