2016-12-12 5 views
-1

外部JSONファイルからデータを解析するajax呼び出しがあります。返されるオブジェクト内のキーの1つはshow_on_mapです。そのキーの値が0の場合は、Googleマップのピンのリストから除外する必要があります。ここでGoogleマップV3 - jsonフィールドに特定の値がある場合、ピンリストから場所を除外します

var map; 

function initialize() { 
    var mapOptions = { 
     center: new google.maps.LatLng(52.4357808, 4.991315699999973), 
     zoom: 2, 
     mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 
} 

var seg = { 
    1: 'invesCastProd', 
    2: 'forged', 
    3: 'airframe', 
    5: 'corp', 
    6: 'structurals' 
} 

var comp = { 
    1: 'structurals', 
    2: 'airfoils', 
    3: 'airfoils', 
    4: 'wyman', 
    5: 'energy', 
    6: 'strucComp', 
    7: 'mechHard', 
    8: 'engineProd', 
    9: 'corp', 
    10: 'aero', 
    12: 'timet', 
    13: 'specMetals' 
} 

$(document).ready(function(){ 
    var myJSON = {}; 
    var myMarkers=[]; 

    $.getJSON("/pcc-common/static/pcc-locations.json", function(json1) { 
     myJSON=json1; 
     $.each(json1, function(key, data) { 
      var latLng = new google.maps.LatLng(data.latitude, data.longitude); 
      var marker = new google.maps.Marker({ 
       position: latLng 
      }); 
      myMarkers[key]=marker; 
      marker.setMap(map); 

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

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

       if (infoWindow) {infoWindow.close();} 
       infoWindow = new google.maps.InfoWindow({ 
        content: "<h5>" + data.display_name + "</h5>" + 
        "<div>" + data.street+ "</div>" + 
        "<div>" + data.city + ", " + data.state + " &nbsp; " + data.postal_code + "</div>" + 
        "<div class='mapPhoneNum'>" + data.telephone + "</div>" + 
        "<a href=" + data.web_url + ">Website</a>" 
       }); 
       infoWindow.open(map, marker); 
       map.setZoom(15); 
       map.panTo(this.getPosition()); 

       google.maps.event.addListener(infoWindow,'closeclick',function(){ 
        resetMapOrigin(); 
       }); 

      }); 

      filterMarkers = function(category){ 

       var component = category.data("component_id"); 
       var segment = category.data("segment_id") 

       setMapOnAll(null); 
       resetMapOrigin(); 

       var filteredMarkers=[]; 

       $.each(myJSON, function(key, data) { 


        if(typeof(component)!="undefined"){ 

         if((myJSON[key].component_id == component) && (myJSON[key].segment_id == segment)){ 
         filteredMarkers.push(key); 
         } 

        }else{ 
         if(myJSON[key].segment_id == segment){ 
         filteredMarkers.push(key); 
         } 
        } 
       }); 

       for(i=0;i<filteredMarkers.length;i++){ 
        myMarkers[filteredMarkers[i]].setMap(map); 
       } 
      } 

      function setMapOnAll(map) { 
       for (var i = 0; i < myMarkers.length; i++) { 
        myMarkers[i].setMap(map); 
       } 
      } 

      function resetMapOrigin(){ 
       map.setZoom(2); 
       map.setCenter({lat:52.4357808,lng:4.991315699999973}); 
      } 
     }); 
    }); 
}); 

返さのサンプルです:あなたは第2の目的は、"show_on_map" : 0を持って見ることができるように

[ 
    { 
    "id" : 2, 
    "display_name" : "PCC Structurals Division Headquarters", 
    "full_address" : "9200 SE Sunnybrook, Suite 240; Clackamas, OR 97015", 
    "latitude" : 45.43, 
    "longitude" : -122.569, 
    "telephone" : "503-777-3881\r\n", 
    "web_url" : "http://www.pccstructurals.com/", 
    "created_at" : "2011-06-08 16:40:13", 
    "segment_id" : 1, 
    "component_id" : 1, 
    "region_id" : 10, 
    "street" : "9200 SE Sunnybrook Drive", 
    "city" : "Portland", 
    "state" : "OR", 
    "country" : "US", 
    "postal_code" : "97015", 
    "employees" : 0, 
    "blurb" : null, 
    "manufacturing_locations" : 0, 
    "other_locations" : 0, 
    "show_on_map" : 1, 
    "contact_info" : null, 
    "component_type_id" : null, 
    "sales_telephone" : null 
    }, 
    { 
    "id" : 3, 
    "display_name" : "AlloyWorks, LLC", 
    "full_address" : "Salisbury, North Carolina 28144\r\n", 
    "latitude" : 35.6735, 
    "longitude" : -80.4774, 
    "telephone" : "704-645-0511\r\n", 
    "web_url" : "", 
    "created_at" : "2011-08-15 14:02:24", 
    "segment_id" : 1, 
    "component_id" : 1, 
    "region_id" : 13, 
    "street" : "814 West Innes Street", 
    "city" : "Salisbury", 
    "state" : "NC", 
    "country" : "US", 
    "postal_code" : "28144", 
    "employees" : null, 
    "blurb" : null, 
    "manufacturing_locations" : 0, 
    "other_locations" : 0, 
    "show_on_map" : 0, 
    "contact_info" : null, 
    "component_type_id" : null, 
    "sales_telephone" : null 
    } 
] 

は、ここに私のGoogleは、スクリプト(その長いこと申し訳ありませんが)にマッピングされます。マップピンから除外する必要があります。これどうやってするの?

おかげ

答えて

1

は、あなただけのデータにfilter機能を使用することができます - 例えば

json = [{foo:1, show_on_map:1},{foo:2, show_on_map:0}]; 
 
filtered = json.filter(function(k) {return k.show_on_map ==1;}) 
 
console.log(filtered);

+0

もJSONファイル内の約185のアドレスオブジェクトがあります。これは、正しい除外を厳密にコーディングする必要があるようです。 – agon024

+0

フィルタは配列の各オブジェクトを反復し、 'show_on_map == 1 'のオブジェクトを除いて新しい配列を返します。したがって、そのプロパティに基づいて除外する指定を超えるハードコーディングはありません。配列内の各オブジェクトにいくつのプロパティが設定されているか心配することなく 'filter'を使うことができます。 –

+0

ああ!とった。私はもはや私のマシンではないので、私は明日それを試さなければならないでしょう。早速のご返事ありがとうございます。私はこれが動作すると確信しています! – agon024

関連する問題