1

我的 Jquery Mobile 页面上有一个表单,用户可以在其中插入起始位置和目的地。

这些值将保存在本地存储中。现在我正在尝试像这样操作输入:

当您在输入框中键入(例如)“当前”时,用户的位置将被放置在本地存储数据库中,而不是“当前”一词。

我已经制作了这个脚本来获取用户的位置:

var geocoder;

 if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} 
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}

function errorFunction(){
alert("Geocoder failed");
}

function initialize() {
geocoder = new google.maps.Geocoder();
 }

function codeLatLng(lat, lng) {

var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
    console.log(results)
        if (results[1]) {
        //formatted address
        document.getElementById('formatedAddress').innerHTML = results[0].formatted_address;
        //find country name
            for (var i=0; i<results[0].address_components.length; i++) {
            for (var b=0;b<results[0].address_components[i].types.length;b++) {

                //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                //this is the object you are looking for
                city= results[0].address_components[i];
                break;
                }
            }
        }
    }
    }
}
);
}

2个输入字段:

<label for="start">Startlocation:</label>
<input type="text" name="start" value="" id="start"/>

<label for="destination">Destination:</label>
<input type="text" name="destination" value="" id="destination"/>

我对 javascript 和 jQuery 非常陌生,我似乎无法找到解决问题的方法。我希望有人能帮助我。谢谢!

4

1 回答 1

0
var start = document.getElementById('start').value;

if (start == 'current') {
    //set lat & long to current
}
else {
    //read lat & long from input fields
}

这一点逻辑可能证明是你所追求的——但我确实倾向于同意逻辑黑猩猩的观点,即用户体验非常重要,并且“使用当前位置”按钮可能对用户更有用。

于 2012-11-08T20:49:59.453 回答