Tôi là một người mới làm quen với nội dung javascript và hiện đang giả mạo nó cho đến khi tôi làm cho nó lol và bây giờ ive đi qua một ngọn đồi nhỏ mà tôi đang đấu tranh để vượt qua: S.Cập nhật vị trí đánh dấu Google Maps V3
Hiện tại tập lệnh của tôi tìm thấy vị trí của người dùng và thêm một ghim vào bản đồ trong khi sao chép LatLon vào một số trường biểu mẫu.
Ngoài việc chỉ phóng to vị trí người dùng, tôi muốn họ có khả năng thêm địa chỉ tùy chỉnh được nhập vào trường văn bản, mã hóa địa lý và sau đó cập nhật ghim hiện tại trên bản đồ.
Tất cả đều hoạt động, mặc dù nó thêm một ghim bổ sung vào bản đồ thay vì cập nhật ghim hiện tại.
Tôi không chắc chắn cách chuyển giá trị từ chức năng mã hóa địa lý địa chỉ trở lại vào ghim gốc/hoặc tôi có xóa ghim gốc và thêm ghim mới không. Tôi chắc chắn rằng tôi có thể tái sử dụng một số chức năng cũng ... tôi không nghĩ rằng mã của tôi là terribly hiệu quả:/
Bất kỳ cách nào tôi hy vọng một guru ra có thể giúp tôi ra
Cheers Nick
var geocoder;
var map;
var pos;
function initialize() {
geocoder = new google.maps.Geocoder();
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var address = document.getElementById("address").value;
var initialLocation;
var myOptions = {
zoom: 12,
center: initialLocation,
mapTypeId: google.maps.MapTypeId.TERRAIN
}
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var marker = new google.maps.Marker({
map: map,
position: pos,
title: 'Location found using HTML5.',
draggable: true
});
var lat = position.coords.latitude
var lng = position.coords.longitude
document.getElementById('geo_latitude').value=lat;
document.getElementById('geo_longitude').value=lng;
google.maps.event.addListener(marker, "dragend", function(event) {
var lat = event.latLng.lat()
var lng = event.latLng.lng()
var infowindow = new google.maps.InfoWindow({
content: '<b><?php _e('Latitude:');?></b>' + lat + '<br><b><?php _e('Longitude:');?></b>' + lng
});
infowindow.open(map, marker);
google.maps.event.addListener(marker, "dragstart", function() {
infowindow.close();
});
document.getElementById('geo_latitude').value=lat;
document.getElementById('geo_longitude').value=lng;
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else if (google.gears) {
browserSupportFlag = true;
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
map.setCenter(initialLocation);
}, function() {
handleNoGeoLocation(browserSupportFlag);
});
// Browser doesn't support Geolocation
} else {
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
function handleNoGeolocation(errorFlag) {
if (errorFlag == true) {
alert("Geolocation service failed.");
initialLocation = newyork;
} else {
alert("Your browser doesn't support geolocation. We've placed you in New York.");
initialLocation = newyork;
}
map.setCenter(initialLocation);
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
//-------------------------------------------------------End initialize
function findAddress(address) {
var address = document.getElementById("address").value;
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var pos = results[0].geometry.location;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
http: // stacko verflow.com/questions/6190482/google-maps-api-v3-update-marker –