0

サーバーからデータを取得し、JSON応答からGoogleマップに複数のマーカーを追加するWebアプリを構築しようとしています。JSON応答は次のようになります。

{"key":[{"Latitude":"60.186518","Longitude":"24.950575"}...]}

この投稿のコードを実装しようとしましたが、動作させることができません。

これはこれまでの私のコードです:

    function success(position) {
  var mapcanvas = document.createElement('div');
  mapcanvas.id = 'mapcontainer';
  mapcanvas.style.height = '600px';

  document.querySelector('article').appendChild(mapcanvas);
  var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  var options = {
    zoom: 15,
    center: coords,
      panControl: false,
  zoomControl: false,
  mapTypeControl: false,
  scaleControl: false,
  streetViewControl: false,
  overviewMapControl: true,

    navigationControlOptions: {
      style: google.maps.NavigationControlStyle.SMALL
    },
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("mapcontainer"), options);
  var marker = new google.maps.Marker({
      position: coords,
      map: map,
      title:"You are here!"});

 function getLocations() {

    $.getJSON("http://myurl.com/jsonresponse", function (json) {

        var location;


        $.each(json.key, function (i, item) {
            addMarker(item.Latitude,item.Longitude);
        });

    });
}

function addMarker(lat,lng) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat,lng),
            map: map,
        });
        markersArray.push(marker);
}

}
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success);
} else {
  error('Geo Location is not supported');
}
4

1 に答える 1

1

これを試してください:

      function getLocations() {
            $.ajax({
              url: 'http://myurl.com/jsonresponse',
              async:false,
              success: function(data) {
                 $.each(data.key, function(index) {
                    addMarker(data[index].Latitude,data[index].Longitude);
                  });
              }
            }); 
        }

それ以外の場合は、addMarker関数でconsole.log(lat、lng)を使用し、出力をここにコピーします。

于 2013-03-26T10:32:27.303 に答える