phpでプログラムを初めて3ヶ月くらいの初心者です。
以下のようなviewで中心位置を動かした場合Upload
ボタンを押すとPHP
のコントローラに中心位置の緯度経度の値が送られる仕組みにしているます。
しかし、center_changed
イベントを起こさせずにUpload
ボタンを押すと、
location.lat is not a function
とコンソールにエラーが出て緯度経度が取得できないようです。
自分はcenter_changed
イベントが起きない場合、最初に与えた値
location = {lat:35.681167, lng: 139.767052};
がそのまま使われるかと期待していたのですが、そうならないようです。
なぜでしょうか?
理由と、もしうまい書き方があればご教授お願いいたします。
<div id="map" style="width:370px;height:370px;"></div> {!! Form::open(['route' => ['microposts.store'], 'method' => 'POST', 'files' => true]) !!} {!! Form::submit('Upload', ['class' => 'btn btn-warning', 'id' => 'button']) !!} {!! Form::hidden('lat') !!} {!! Form::hidden('long') !!} {!! Form::close() !!}
window.onload=function() { var location = {lat:35.681167, lng: 139.767052}; var options = { zoom: 14, center: location, disableDoubleClickZoom: true }; var map = new google.maps.Map(document.getElementById('map'), options); var marker=new google.maps.Marker({position: location,map: map,}); google.maps.event.addListener(map, 'center_changed', function(){ location = map.getCenter(); marker.setPosition(location); console.log(location.lat(),location.lng()); }); $('form').submit(function(){ $('[name="lat"]').val(location.lat()); $('[name="long"]').val(location.lng()); }); }
回答2件
あなたの回答
tips
プレビュー