Tôi đang cố gắng tự động thay đổi khu vực tùy thuộc vào những gì tôi chọn trong trường thả xuống.
Ai đó có thể giúp tôi tìm ra điều này không?
hỏi ngày 9 tháng 4 năm 2015 lúc 6:15Apr 9, 2015 at 6:15
2
Bạn có thể tải JavaScript một cách linh hoạt sau khi trang đã tải, nhưng hãy nhớ: Một khi bạn đã tải nó, bạn không thể dỡ JavaScript trong một trang hoạt động. Nó được lưu trữ trong nhóm bộ nhớ trình duyệt. Bạn có thể tải lại một trang, sẽ xóa các tập lệnh hoạt động và bắt đầu lại. Ngoài ra, bạn có thể ghi đè các chức năng mà bạn đã đặt.
Với điều này đã nói. Đây là cách thay đổi tập lệnh sau khi tải trang với JavaScript:
Select region
Sweden
Denmark
function changeRegion[value]
{
var s = document.createElement["script"];
s.type = "text/javascript";
s.src = "//maps.googleapis.com/maps/api/js?region=" + value;
s.innerHTML = null;
s.id = "map";
document.getElementById["output"].innerHTML = "";
document.getElementById["output"].appendChild[s];
}
Đã trả lời ngày 9 tháng 4 năm 2015 lúc 6:40Apr 9, 2015 at 6:40
Henrikhenrikhenrik
1.5133 huy hiệu vàng13 Huy hiệu bạc29 Huy hiệu đồng3 gold badges13 silver badges29 bronze badges
Bạn nên tránh tải API Google Maps nhiều lần. Nếu có thể, bạn nên xem xét để lại thẻ tập lệnh đó và thay vào đó thêm nó thông qua JavaScript sau khi lựa chọn thả xuống [vùng] đã được thực hiện.
EDIT:
Giả sử bạn có một thả xuống như thế này:
Select region to use Google Maps:
Denmark
Sweden
Thêm tập lệnh sẽ giống như:
function loadGoogleMaps[] {
var selectedRegion = document.getElementById["regionSelector"].value;
if[selectedRegion === ''] {
return;
}
var head= document.getElementsByTagName['head'][0];
var script= document.createElement['script'];
script.type= 'text/javascript';
script.src= '//maps.googleapis.com/maps/api/js?region=' + selectedRegion;
head.appendChild[script];
}
Thông tin thêm về tải API của Google Maps: //developers.google.com/maps/documentation/javascript/examples/map-simple-async
Đã trả lời ngày 9 tháng 4 năm 2015 lúc 6:43Apr 9, 2015 at 6:43
Ted Nyberged NybergTed Nyberg
6.4877 Huy hiệu vàng39 Huy hiệu bạc69 Huy hiệu Đồng7 gold badges39 silver badges69 bronze badges
1
Thử cái này..
Google Maps Autocomplete Search Sample
$[document].ready[function[]{
var mapOptions = {
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng[41.06000,28.98700]
};
var map = new google.maps.Map[document.getElementById["map"],mapOptions];
var geocoder = new google.maps.Geocoder[];
$[function[] {
$["#searchbox"].autocomplete[{
source: function[request, response] {
if [geocoder == null]{
geocoder = new google.maps.Geocoder[];
}
geocoder.geocode[ {'address': request.term }, function[results, status] {
if [status == google.maps.GeocoderStatus.OK] {
var searchLoc = results[0].geometry.location;
var lat = results[0].geometry.location.lat[];
var lng = results[0].geometry.location.lng[];
var latlng = new google.maps.LatLng[lat, lng];
var bounds = results[0].geometry.bounds;
geocoder.geocode[{'latLng': latlng}, function[results1, status1] {
if [status1 == google.maps.GeocoderStatus.OK] {
if [results1[1]] {
response[$.map[results1, function[loc] {
return {
label : loc.formatted_address,
value : loc.formatted_address,
bounds : loc.geometry.bounds
}
}]];
}
}
}];
}
}];
},
select: function[event,ui]{
var pos = ui.item.position;
var lct = ui.item.locType;
var bounds = ui.item.bounds;
if [bounds]{
map.fitBounds[bounds];
}
}
}];
}];
}];
Đã trả lời ngày 9 tháng 4 năm 2015 lúc 6:22Apr 9, 2015 at 6:22
2