【问题标题】:Laravel form request with geocoder带有地理编码器的 Laravel 表单请求
【发布时间】:2016-07-26 16:13:15
【问题描述】:

我的视图中有一个由复选框、一些文本字段和选择组成的表单。

在同一页面上,我有 googlemap 地理编码器。位置 lat&lng 保存在 javascript 的变量中。

function geocodeAddress(geokodiranje, resultsMap) {
    var address = document.getElementById('address').value;
    var location = {};
    geokodiranje.geocode({'address': address}, function(results, status) {
        if (status === google.maps.GeocoderStatus.OK) {

            location.lattitude = results[0].geometry.location.lat();
            location.longitude = results[0].geometry.location.lng();

            resultsMap.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: resultsMap,
                position: results[0].geometry.location
            });
            lat = location.lattitude;  //THIS IS MY LAT
            lng = location.longitude; //THIS IS MY LNG 
        } else {
            alert('Geocode failed for reason: ' + status);
        }

    });

}

在我的控制器中,我有 POST 功能可以将表单保存在数据库中。

$input = Request::all();
Blogpost::create($input);
return redirect('other');

在我的数据库中,我有 lat 和 lng 列,我想添加变量,问题是我无法将这 2 个变量添加到控制器中的 $input 变量中。请帮帮我! :)

【问题讨论】:

    标签: javascript php google-maps laravel-5 request


    【解决方案1】:

    您需要在表单中添加 2 个隐藏字段 lat 和 lon,例如:

    <input type="hidden" name="lat" class="lat" />
    <input type="hidden" name="lon" class="lon" />
    

    并像这样在js代码中设置值:

    function geocodeAddress(geokodiranje, resultsMap) {
    var address = document.getElementById('address').value;
    var location = {};
    geokodiranje.geocode({'address': address}, function(results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
    
            location.lattitude = results[0].geometry.location.lat();
            location.longitude = results[0].geometry.location.lng();
    
            resultsMap.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: resultsMap,
                position: results[0].geometry.location
            });
            lat = location.lattitude;  //THIS IS MY LAT
            lng = location.longitude; //THIS IS MY LNG 
            $('.lat').val(lat);
            $('.lon').val(lng);
        } else {
            alert('Geocode failed for reason: ' + status);
        }
    
    });
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2019-10-12
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      相关资源
      最近更新 更多