2017-01-21 4 views
1

마커. 이 기능으로 원하는 것은 내 Google지도에 마커를 추가하는 것입니다. 이 기능을 가정 해 봅시다 나는 다음과 같은 JSON의 reponse를 얻을 :설정 입력지도에 대한 동적 VAR 나는 다음과 같은 기능을 가지고

{"status":0,"data":[{"spid":"NL-351202","name":"In 't vosje","add":"Nobelstraat 141","zip":"3512EM","city":"UTRECHT","country":"NL","lon":"5.1246600000","lat":"52.0929748000"},{"spid":"NL-358102","name":"Vivant Verheijen","add":"Burg Reigerstraat 54","zip":"3581KV","city":"UTRECHT","country":"NL","lon":"5.1358400000","lat":"52.0895721000"},{"spid":"NL-351175","name":"Parcelstation deBuren H.Catharijne","add":"Catharijnesingel 11-PS","zip":"3511GB","city":"UTRECHT","country":"NL","lon":"5.1115500000","lat":"52.0922582000"},{"spid":"NL-351403","name":"Vivant De Molen","add":"Adelaarstraat 32","zip":"3514CG","city":"UTRECHT","country":"NL","lon":"5.1158800000","lat":"52.0993884000"},{"spid":"NL-358303","name":"Qualitours","add":"Adr. Van Ostadelaan 41","zip":"3583AA","city":"UTRECHT","country":"NL","lon":"5.1396700000","lat":"52.0832667000"},{"spid":"NL-353103","name":"Ina Ugas 2","add":"Damstraat 64","zip":"3531BW","city":"UTRECHT","country":"NL","lon":"5.0000","lat":"52.0922908000"},{"spid":"NL-352203","name":"Cigars N More Utrecht","add":"Rijnlaan 12","zip":"3522BN","city":"UTRECHT","country":"NL","lon":"5.1145300000","lat":"52.0769769000"},{"spid":"NL-357101","name":"Foto Ben Romp","add":"Troosterhof 1","zip":"3571NC","city":"UTRECHT","country":"NL","lon":"5.1356800000","lat":"52.1064315000"},{"spid":"NL-353203","name":"Buurtwinkel Nieuw England","add":"Vleutenseweg 355","zip":"3532HH","city":"UTRECHT","country":"NL","lon":"5.0961200000","lat":"52.0937046000"},{"spid":"NL-355102","name":"Vivant De Plantage","add":"Amsterdamsestraatweg 279","zip":"3551CE","city":"UTRECHT","country":"NL","lon":"5.0989600000","lat":"52.1015290000"},{"spid":"NL-352301","name":"Boek en Kantoor Robman","add":"Smaragdplein 224","zip":"3523EH","city":"UTRECHT","country":"NL","lon":"5.1248600000","lat":"52.0721031000"},{"spid":"NL-356202","name":"The Cartridgers Overvecht","add":"Zamenhofdreef 43","zip":"3562JT","city":"UTRECHT","country":"NL","lon":"5.1120700000","lat":"52.1144018000"},{"spid":"NL-355301","name":"Drogisterij de Kamil","add":"Amsterdamsestraatweg 524","zip":"3553EN","city":"UTRECHT","country":"NL","lon":"5.0844700000","lat":"52.1094875000"},{"spid":"NL-352875","name":"Parcelstation deBuren Utrecht","add":"Papendorpseweg 95-PS","zip":"3528BJ","city":"UTRECHT","country":"NL","lon":"5.0881500000","lat":"52.0629634000"},{"spid":"NL-354203","name":"DHL Parcel Hoofdkantoor","add":"Reactorweg 25","zip":"3542AD","city":"Utrecht","country":"NL","lon":"5.0640800000","lat":"52.1049812000"},{"spid":"NL-354251","name":"DHL Parcelstation Lage weide","add":"Reactorweg 25-PS","zip":"3542AD","city":"UTRECHT","country":"NL","lon":"5.0640800000","lat":"52.1049812000"},{"spid":"NL-354244","name":"Test shop","add":"Reactorweg 25","zip":"3542AD","city":"Utrecht","country":"NL","lon":"5.0640800000","lat":"52.1049812000"},{"spid":"NL-373104","name":"CIGO De Bilt","add":"Hessenweg 162","zip":"3731JN","city":"DE BILT","country":"NL","lon":"5.1817900000","lat":"52.1103054000"},{"spid":"NL-354301","name":"G&W Gezondheidswinkel De Weegschaal","add":"Ella Fitzgeraldplein 34","zip":"3543EP","city":"UTRECHT","country":"NL","lon":"5.0463800000","lat":"52.1015022000"},{"spid":"NL-354202","name":"DHL Parcel Utrecht","add":"Rutherfordweg 1","zip":"3542CN","city":"UTRECHT","country":"NL","lon":"5.0561100000","lat":"52.1204578000"}]} 

내가 지금 원하는 것은 내 스크립트 내지도에 마커를 것 있도록 VAR에이 데이터를 배치하는 것입니다. 예는 다음과 같습니다 (그러나 참고 : 나는 정적이 지금, 각 시간이해서 getJSON와 다른 우편 번호에이 채워집니다처럼 싶지 않아,이 VAR를 업데이트 할 수 있습니다.)

var stores = { 
    "type": "FeatureCollection", 
    "features": [ 
     { 
      "type": "Feature", 
      "geometry": { 
       "type": "Point", 
       "coordinates": [5.140439, 51.686608] 
      }, 
      "properties": { 
       "spid": "NL3700-03", 
       "name": "Test Parcel 1", 
       "add": "Teststreet 1", 
       "zip": "3500RR", 
       "city": "utrecht", 
       "country": "NL", 
       "phone": "032434234", 
       "oh": "Ma: 12:00 tot 15:00" 
      } 
     }, 
     { 
      "type": "Feature", 
      "geometry": { 
       "type": "Point", 
       "coordinates": [5.133729,51.681425] 
      }, 
      "properties": { 
       "phoneFormatted": "(202) 507-8357", 
       "phone": "2025078357", 
       "add": "2221 I St NW", 
       "city": "Washington DC", 
       "country": "United States", 
       "crossStreet": "at 22nd St NW", 
       "postalCode": "20037", 
       "state": "D.C." 
      } 
     } 
    ] 
}; 

내 질문을 : 어떻게해야합니까? 나는 JSON 출력 값이 VAR STORES의 "name"에 있는지 확인하려고합니다. 그리고 이것은 각 상점을위한 것입니다.

답변

0

당신은 객체로 JSON을로드 할 수 있습니다 당신이가는대로 자신의 데이터 구조에 추가, 그리고 결과를 통해 단지 루프를 첫 번째 기능

var places = {}; 

$(".mijn_veld").change(function() { 
    $.getJSON('https://url.com/index.php?action=pslist&zip='+$(".mijn_veld").val()+'&country=NL', function(result) { 
     places = result; 
    }); 
}); 

를 사용하여 places을했다. 이 예에서는 장소를 수동으로 설정하지만 용도에 맞게 서버에서로드 할 수 있습니다.

var places = { 
    "status":0, 
    "data":[ 
     { 
     "spid":"NL-351202", 
     "name":"In 't vosje", 
     "add":"Nobelstraat 141", 
     "zip":"3512EM", 
     "city":"UTRECHT", 
     "country":"NL", 
     "lon":"5.1246600000", 
     "lat":"52.0929748000" 
     }, 
     { 
     "spid":"NL-358102", 
     "name":"Vivant Verheijen", 
     "add":"Burg Reigerstraat 54", 
     "zip":"3581KV", 
     "city":"UTRECHT", 
     "country":"NL", 
     "lon":"5.1358400000", 
     "lat":"52.0895721000" 
     }, 
     { 
     "spid":"NL-351175", 
     "name":"Parcelstation deBuren H.Catharijne", 
     "add":"Catharijnesingel 11-PS", 
     "zip":"3511GB", 
     "city":"UTRECHT", 
     "country":"NL", 
     "lon":"5.1115500000", 
     "lat":"52.0922582000" 
     } 
    ] 
}; 

var stores = { 
    "type": "FeatureCollection", 
    "features": [] 
}; 

for (var i = 0; i < places.data.length; i++) { 
    var place = places.data[i]; 

    stores.features.push({ 
     "type": "Feature", 
     "geometry": { 
      "type": "Point", 
      "coordinates": [place.lat, place.lon] 
     }, 
     properties: { 
     "spid": place.spid, 
     "name": place.name, 
      "add": place.add, 
      "zip": place.zip, 
      "city": place.city, 
      "country": place.country, 
     } 
    }); 
} 

console.log(stores); 
+0

다음과 같은 오류가 나타납니다. TypeError : places.data is undefined – nhatimme

+0

앞서 말한대로 places.data는 정의되지 않았습니다. 나는 콘솔이 내게 말하는 것을 점검했다. Object와 배열을 보았다. Object {status : 0, data : Array [20]} – nhatimme

+0

나는 더 테스트를 마쳤다. 수동으로 설정 한 var 장소를 삭제했습니다. 그런 다음 이전에 말한 오류가 발생합니다. – nhatimme