2016-10-24 15 views
-1

データを呼び出すために$ getJSONメソッドを使用しました。マーカーはマップ上に完全に表示されていますが、マーカーをクリックすると値が表示されません。だから、私のサーバー上にあるJSONデータはここにありますが、私はLinkとJSONデータのコードのいくつかを自分のHTMLとJavaScriptと共に与えています。Google MapのJSONからInfowindowデータが表示されない

var map,infowindow; 
 
    
 
    function initialize() { 
 
    
 
     var mapProp = { 
 
       center: new google.maps.LatLng(28.003389000000, -82.429500000000), 
 
       zoom: 10, 
 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
 
      }; 
 

 
     map = new google.maps.Map(document.getElementById("map"), mapProp); 
 
     $.getJSON('js/file.json', function (json1) { 
 
    
 
      $.each(json1.ResponseData, function (key, data) { 
 

 
       var latLng = new google.maps.LatLng(data.CoordinateY, data.CoordinateX), 
 
        marker = new google.maps.Marker({ 
 
         position: latLng, 
 
         map: map, 
 
         title: data.NatureId 
 
        }); 
 
        
 
       
 
      }); 
 
      
 
    var clicker = addClicker(marker, data.NatureId); 
 
    
 

 
     }); 
 
       function addClicker(marker, content) { 
 
    google.maps.event.addListener(marker, 'click', function() { 
 
     
 
     if (infowindow) {infowindow.close();} 
 
     infowindow = new google.maps.InfoWindow({content: content}); 
 
     infowindow.open(map, marker); 
 
     
 
    }); 
 
    } 
 
    } 
 
    
 
    google.maps.event.addDomListener(window, 'load', initialize);
<script type="text/javascript" src = "http://maps.google.com/maps/api/js?sensor=false"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="map" style="border: 2px solid #3872ac;"></div>

、ここでJSONファイルのコードです。ここで

{ 
 
    "OpperationErrorMsg":"", 
 
    "IsSuccess":true, 
 
    "ResultId":1000, 
 
    "Timestamp":"2016-10-12T18:00:07.0232702Z", 
 
    "Echo":null, 
 
    "InSandbox":true, 
 
    "DebugMessages":[ 
 

 
    ], 
 
    "MissingDetails":[ 
 

 
    ], 
 
    "ResponseData":[ 
 
     { 
 
     "CallTimeLocal":"2016-10-10T06:28:48.7330000", 
 
     "IncidentId":3374, 
 
     "IncidentNumber":"HC2016004034", 
 
     "CallTime":"2016-10-10T10:28:48.7330000", 
 
     "ElapsedSeconds":0, 
 
     "Location":"2712 E HANNA AVE", 
 
     "BuildingName":null, 
 
     "BuildingNumber":null, 
 
     "NatureId":6743, 
 
     "FirePriorityId":1, 
 
     "CoordinateX":-82.429500000000, 
 
     "CoordinateY":28.003389000000 
 
     }, 
 
     { 
 
     "CallTimeLocal":"2016-10-10T11:28:36.7000000", 
 
     "IncidentId":3382, 
 
     "IncidentNumber":"HC2016004042", 
 
     "CallTime":"2016-10-10T15:28:36.7000000", 
 
     "ElapsedSeconds":0, 
 
     "Location":"1220 APOLLO BEACH BLVD S", 
 
     "BuildingName":"Apollo Beach Marina", 
 
     "BuildingNumber":null, 
 
     "NatureId":8035, 
 
     "FirePriorityId":1, 
 
     "CoordinateX":-82.422369000000, 
 
     "CoordinateY":27.781254000000 
 
     }, 
 
     { 
 
     "CallTimeLocal":"2016-10-10T14:29:59.8830000", 
 
     "IncidentId":3387, 
 
     "IncidentNumber":"HC2016004047", 
 
     "CallTime":"2016-10-10T18:29:59.8830000", 
 
     "ElapsedSeconds":0, 
 
     "Location":"9600 SHELDONWOOD RD", 
 
     "BuildingName":null, 
 
     "BuildingNumber":null, 
 
     "NatureId":6420, 
 
     "FirePriorityId":12, 
 
     "CoordinateX":-82.580530000000, 
 
     "CoordinateY":28.034779000000 
 
     }, 
 
     { 
 
     "CallTimeLocal":"2016-10-10T15:27:37.7270000", 
 
     "IncidentId":3389, 
 
     "IncidentNumber":"HC2016004049", 
 
     "CallTime":"2016-10-10T19:27:37.7270000", 
 
     "ElapsedSeconds":0, 
 
     "Location":"4691 GALLAGHER RD", 
 
     "BuildingName":"Strawberry Crest High School", 
 
     "BuildingNumber":null, 
 
     "NatureId":7873, 
 
     "FirePriorityId":2, 
 
     "CoordinateX":-82.236450000000, 
 
     "CoordinateY":28.021233000000 
 
     } 
 
    ], 
 
    "CurrentStatusData":null 
 
}
マーカーをクリックすると、ビル名を表示したい場所、NaturalIDです。

ありがとうございました。

答えて

0

ライン:

var clicker = addClicker(marker, data.NatureId); 

は間違った場所にあります。あなたは

proof of concept fiddle

$.each(jsonData.ResponseData, function(key, data) { var latLng = new google.maps.LatLng(data.CoordinateY, data.CoordinateX), var marker = new google.maps.Marker({ position: latLng, map: map, title: ""+data.NatureId }); var clicker = addClicker(marker, ""+data.NatureId); }); 

.each関数内)作成された各マーカーのためにそれを実行するために必要なコードスニペット:

var map, infowindow; 
 

 
function initialize() { 
 

 
    var mapProp = { 
 
    center: new google.maps.LatLng(28.003389000000, -82.429500000000), 
 
    zoom: 10, 
 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }; 
 

 
    map = new google.maps.Map(document.getElementById("map"), mapProp); 
 
    // $.getJSON('js/file.json', function(json1) { 
 

 
    $.each(jsonData.ResponseData, function(key, data) { 
 

 
    var latLng = new google.maps.LatLng(data.CoordinateY, data.CoordinateX), 
 
     marker = new google.maps.Marker({ 
 
     position: latLng, 
 
     map: map, 
 
     title: "" + data.NatureId 
 
     }); 
 
    console.log(latLng.toUrlValue(6) + ":" + data.NatureId) 
 
    var clicker = addClicker(marker, "" + data.NatureId); 
 

 
    }); 
 

 

 

 

 
    // }); 
 

 
    function addClicker(marker, content) { 
 
    google.maps.event.addListener(marker, 'click', function() { 
 

 
     if (infowindow) { 
 
     infowindow.close(); 
 
     } 
 
     infowindow = new google.maps.InfoWindow({ 
 
     content: content 
 
     }); 
 
     infowindow.open(map, marker); 
 

 
    }); 
 
    } 
 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize); 
 
var jsonData = { 
 
    "OpperationErrorMsg": "", 
 
    "IsSuccess": true, 
 
    "ResultId": 1000, 
 
    "Timestamp": "2016-10-12T18:00:07.0232702Z", 
 
    "Echo": null, 
 
    "InSandbox": true, 
 
    "DebugMessages": [ 
 

 
    ], 
 
    "MissingDetails": [ 
 

 
    ], 
 
    "ResponseData": [{ 
 
    "CallTimeLocal": "2016-10-10T06:28:48.7330000", 
 
    "IncidentId": 3374, 
 
    "IncidentNumber": "HC2016004034", 
 
    "CallTime": "2016-10-10T10:28:48.7330000", 
 
    "ElapsedSeconds": 0, 
 
    "Location": "2712 E HANNA AVE", 
 
    "BuildingName": null, 
 
    "BuildingNumber": null, 
 
    "NatureId": 6743, 
 
    "FirePriorityId": 1, 
 
    "CoordinateX": -82.429500000000, 
 
    "CoordinateY": 28.003389000000 
 
    }, { 
 
    "CallTimeLocal": "2016-10-10T11:28:36.7000000", 
 
    "IncidentId": 3382, 
 
    "IncidentNumber": "HC2016004042", 
 
    "CallTime": "2016-10-10T15:28:36.7000000", 
 
    "ElapsedSeconds": 0, 
 
    "Location": "1220 APOLLO BEACH BLVD S", 
 
    "BuildingName": "Apollo Beach Marina", 
 
    "BuildingNumber": null, 
 
    "NatureId": 8035, 
 
    "FirePriorityId": 1, 
 
    "CoordinateX": -82.422369000000, 
 
    "CoordinateY": 27.781254000000 
 
    }, { 
 
    "CallTimeLocal": "2016-10-10T14:29:59.8830000", 
 
    "IncidentId": 3387, 
 
    "IncidentNumber": "HC2016004047", 
 
    "CallTime": "2016-10-10T18:29:59.8830000", 
 
    "ElapsedSeconds": 0, 
 
    "Location": "9600 SHELDONWOOD RD", 
 
    "BuildingName": null, 
 
    "BuildingNumber": null, 
 
    "NatureId": 6420, 
 
    "FirePriorityId": 12, 
 
    "CoordinateX": -82.580530000000, 
 
    "CoordinateY": 28.034779000000 
 
    }, { 
 
    "CallTimeLocal": "2016-10-10T15:27:37.7270000", 
 
    "IncidentId": 3389, 
 
    "IncidentNumber": "HC2016004049", 
 
    "CallTime": "2016-10-10T19:27:37.7270000", 
 
    "ElapsedSeconds": 0, 
 
    "Location": "4691 GALLAGHER RD", 
 
    "BuildingName": "Strawberry Crest High School", 
 
    "BuildingNumber": null, 
 
    "NatureId": 7873, 
 
    "FirePriorityId": 2, 
 
    "CoordinateX": -82.236450000000, 
 
    "CoordinateY": 28.021233000000 
 
    }], 
 
    "CurrentStatusData": null 
 
};
html, 
 
body, 
 
#map { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map" style="border: 2px solid #3872ac;"></div>

関連する問題