###前提・実現したいこと
CakePHP3でWebシステムの開発をしています。
OpenStreetMapを用いてマップ上にマーカーを表示させたいと思っています。
マップ自体は表示され、consolelogで緯度経度の情報を取得することに成功していることは確認しているのですが、マーカーが描画されません。
ajaxで緯度経度のデータをresponseで取得しています。
よろしくお願いします。
###該当のソースコード
/appname/webroot/js/hoge.js
Javascript
1$(function() { 2 /** 3 * フォームの初期表示 4 * @return {[type]} [description] 5 */ 6 window.onload = function() { 7 //マップ画面表示 8 setMap(); 9 } 10 11 /** 12 * Map関連 13 */ 14 var setMap = function(){ 15 // 地図のデフォルトの緯度経度と拡大率 16 var map = L.map('map').setView([35.681,139.766], 12); 17 //OSMレイヤー追加 18 var tileLayer = L.tileLayer( 19 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { 20 attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a>', 21 maxZoom: 18 22 } 23 ); 24 25 var lat = []; // 緯度 26 var lon = []; // 経度 27 28 //Geocoording処理を呼び、住所から緯度経度を取得する 29 $.ajax({ 30 type:'POST', 31 url:'/ajax/ajax-geocoding', 32 dataType:'json', 33 data:{ 34 prefecture:prefecture_name, 35 city:city_name, 36 town:town_name, 37 block:block_number 38 }, 39 success: function(response){ 40 for(var i in response['latlons']){ 41 lat[i] = response['latlons'][i]['lat']; 42 lon[i] = response['latlons'][i]['lon']; 43 } 44 45 // 緯度経度をGeoJsonに渡す 46 var geojsonFeature = []; 47 geojsonFeature.unshift({ 48 "type": "Feature", 49 "properties": { 50 "marker": "マーカー" 51 }, 52 "geometry": { 53 "type": "Point", 54 "coordinates": [lat[1], lon[1]] 55 } 56 }); 57 // ポップアップに表示する 58 L.geoJson( 59 geojsonFeature,{ 60 onEachFeature: function(feature, layer){ 61 if (feature.properties) { 62 ayer.bindPopup(feature.properties.marker); 63 } 64 } 65 } 66 ).addTo(map); 67 68 //JSON形式の位置情報を設定 69 tileLayer.addTo(map); 70 }, 71 error: function(response){ 72 alert('ajaxGeocoding エラ--'); 73 } 74 }) 75 } 76}); 77
###補足情報
http://unguis.cre8or.jp/web/6341
こちらのサイトを参考にコードを書きました。
// ポップアップに表示する
の直前でgeojsonFeatureを確認したところ、下記の値を取得していました。
Array(1) 0:Object geometry:Object coordinates:Array(2) 0:35.69853 1:139.757108 length:2 __proto__:Array(0) type:"Point" __proto__:Object properties:Object marker:"マーカー" __proto__:Object type:"Feature" __proto__:Object length:1 __proto__:Array(0)
回答2件
あなたの回答
tips
プレビュー