0

我正在尝试将输入到字段street addresscitystatezipcode中的值,然后使用这些值填充longitude和的隐藏字段latitude

我正在尝试做的一个例子可以在这里看到,我只是想用多个输入字段来做。

inside{...}用于value=" "CMS 表达式引擎,因此可以忽略

表单域:

<div class="formsection clearfix">
    <label for="trainer_address">Street Address</label>
    <span class="directions">This will NOT be displayed in your listing. Only for search Purposes.</span>
    <input type="text" name="trainer_address" id="trainer_address" value="{trainer_address}">
</div>

<div class="formsection clearfix">
    <label for="trainer_city">City</label>
    <span class="directions">The city to be displayed in your listing.</span>
    <input type="text" name="trainer_city" id="trainer_city" value="{trainer_city}">
</div>

<div class="formsection clearfix">
    <label for="trainer_state">State</label>
    <span class="directions">The state to be displayed in your listing.</span>
    <select name="trainer_state">
        {options:trainer_state}
            <option value="{option_value}"{selected}>{option_name}</option>
        {/options:trainer_state}
    </select>
</div>

<div class="formsection clearfix">
    <label for="trainer_zip">Zip Code</label>
    <span class="directions">The zip code to be displayed in your listing.</span>
    <input type="text" name="trainer_zip" id="trainer_zip" value="{trainer_zip}">
</div>

以下是隐藏的纬度和经度字段:

<input type="hidden" id="trainer_longitude" name="trainer_longitude" value="{trainer_longitude}" />
<input type="hidden" id="trainer_latitude" name="trainer_latitude" value="{trainer_latitude}" />
4

1 回答 1

1

您可以为此使用谷歌地理编码器。只需在所有地址字段上绑定 onchange,当您拥有完整地址时,您可以执行类似的操作来获取 longlat。顺便说一句,地址格式非常宽容谷歌地图接受的任何值,在这里也是可以接受的。

// 参考 https://developers.google.com/maps/documentation/javascript/reference#LatLng

// 包含脚本 src="https://maps.googleapis.com/maps/api/js?sensor=false">

// the method to get the geocode
var geocoder = new google.maps.Geocoder();

function getLatLng (address, callback){
    geocoder.geocode({ 'address': address}, function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
            callback(results[0].geometry.location)
        }
        else {
            callback(false);
        }
    });
}

// how to use it
getLatLng ("1600 Pennsylvania Ave NW, Washington, DC, United States", function(result){

    if (result == false){ alert("ADDRESS IS NO GOOD"); }
    else {
        alert("Lng: " + result.lng() + " Lat: " +result.lat());
    }    
});
于 2013-03-12T17:24:32.273 回答