![](/img/trans.png)
[英]Is it possible to instantiate a javascript class from a dynamic variable?
[英]Is it possible to store the results from a class to a variable in Javascript
我正在嘗試從本地下面的文本框中存儲緯度和經度值,以便可以將它們實現為ajax函數,但是由於某些原因,它們將不會存儲到局部變量中。
例如,為了在文本框中顯示這兩個值(通過轉換搜索欄中的地址來計算),我使用了以下代碼:
<p>Address: <input type="text" class="search_addr" size="45"/></p>
<p>Latitude: <input type="text" class="search_latitude" size="30"/></p>
<p>Longitude: <input type="text" class="search_longitude" size="30"/></p>
我發現不可能將“ search_latitude”和“ search_longitude”表示的值存儲到局部變量中。 我在下面提供了一段JavaScript代碼,但是我進行的嘗試如下:
// This attempt returns the values as "[Object object]"
var Lat = $('.search_latitude').val(marker.getPosition().lat());
var Long = $('.search_longitude').val(marker.getPosition().lng());
// And this attempt stops the map from loading and stops all functionality
var Lat = $('.search_latitude').val();
var Long = $('.search_longitude').val();
作為javascript的初學者,我已經沒有足夠的想法來解決這個問題。 有小費嗎?
<script>
var geocoder;
var map;
var marker;
/*
* Google Map with marker
*/
function initialize() {
var initialLat = $('.search_latitude').val();
var initialLong = $('.search_longitude').val();
initialLat = initialLat?initialLat:53.350140;
initialLong = initialLong?initialLong:-6.266155;
var latlng = new google.maps.LatLng(initialLat, initialLong);
var options = {
zoom: 11,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("geomap"), options);
geocoder = new google.maps.Geocoder();
marker = new google.maps.Marker({
map: map,
draggable: true,
position: latlng
});
google.maps.event.addListener(marker, "dragend", function () {
var point = marker.getPosition();
map.panTo(point);
geocoder.geocode({'latLng': marker.getPosition()}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker.setPosition(results[0].geometry.location);
$('.search_addr').val(results[0].formatted_address);
$('.search_latitude').val(marker.getPosition().lat());
$('.search_longitude').val(marker.getPosition().lng());
}
});
});
}
$(document).ready(function () {
//load google map
initialize();
/*
* autocomplete location search
*/
var PostCodeid = '#search_location';
$(function () {
$(PostCodeid).autocomplete({
source: function (request, response) {
geocoder.geocode({
'address': request.term
}, function (results, status) {
response($.map(results, function (item) {
return {
label: item.formatted_address,
value: item.formatted_address,
lat: item.geometry.location.lat(),
lon: item.geometry.location.lng()
};
}));
});
},
select: function (event, ui) {
$('.search_addr').val(ui.item.value);
$('.search_latitude').val(ui.item.lat);
$('.search_longitude').val(ui.item.lon);
var latlng = new google.maps.LatLng(ui.item.lat, ui.item.lon);
marker.setPosition(latlng);
initialize();
}
});
});
/*
* Point location on google map
*/
$('.get_map').click(function (e) {
var address = $(PostCodeid).val();
geocoder.geocode({'address': address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker.setPosition(results[0].geometry.location);
$('.search_addr').val(results[0].formatted_address);
$('.search_latitude').val(marker.getPosition().lat());
$('.search_longitude').val(marker.getPosition().lng());
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
e.preventDefault();
});
//Add listener to marker for reverse geocoding
google.maps.event.addListener(marker, 'drag', function () {
geocoder.geocode({'latLng': marker.getPosition()}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$('.search_addr').val(results[0].formatted_address);
$('.search_latitude').val(marker.getPosition().lat());
$('.search_longitude').val(marker.getPosition().lng());
}
}
});
});
});
geocoder.geocode({
'address': request.term,
componentRestrictions: {country: "ie"}
})
function loginAlert(){
alert("User must be logged in to view reports");
}
</script>
jQuery val()
函數返回一個jQuery對象,而不是字符串。 將沒有toString
方法的對象轉換為字符串將顯示為[Object object]
。 似乎您要用一條語句來做兩件事:(1)將緯度/經度寫入HTML元素。 (2)將緯度/經度寫入變量。 嘗試分別執行以下操作:
// (1) Write the latitude into the HTML element.
$('.search_latitude').val(marker.getPosition().lat());
// (2) Write the latitude into a variable.
var Lat = marker.getPosition().lat();
// then perhaps check if the output is what you expected by viewing the developer console
console.log(Lat);
然后類似地表示經度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.