我对保存数据有疑问...它将数据保存到数据库中,并且还在地图上显示,所以基本上一切正常,但是当我保存标记的地址和名称然后单击保存按钮时我无法关闭它...
downloadUrl(url, function(data, responseCode) {
if (responseCode == 200 && data.length <= 1) {
infowindow.close();
document.getElementById("message").innerHTML = "Location added.";
}
从downloadUrl开始有些不好,似乎代码被忽略了,因此我发出警报让用户知道它已保存。
如果有帮助,我会复制整个 javascript(它还包括地理编码器和样式地图):如果你愿意,我可以给你一个指向地图的链接或你需要的任何东西。
var marker;
var infowindow;
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var styles = [
{
"stylers": [
{ "hue": "#0055ff" },
{ "saturation": 90 },
{ "gamma": 0.39 },
{ "lightness": 48 },
{ "visibility": "on" },
{ "invert_lightness": true }
]
},{
"featureType": "road",
"stylers": [
{ "hue": "#ff4500" }
]
},{
"elementType": "labels",
"stylers": [
{ "hue": "#00ff6f" },
{ "gamma": 6.31 },
{ "weight": 0.2 }
]
}
];
var SajatMap = new google.maps.StyledMapType(styles,
{name: "Saját Map"});
var image=new google.maps.MarkerImage('resistance2.png',
new google.maps.Size(23, 30),
new google.maps.Point(0,0),
new google.maps.Point(0, 32));
var latlng = new google.maps.LatLng(47.495632, 19.044748);
var options = {
center: latlng,
zoom: 12,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'sajat_map']
}
};
map = new google.maps.Map(document.getElementById("map"), options);
var infoWindow = new google.maps.InfoWindow();
map.mapTypes.set('sajat_map', SajatMap);
map.setMapTypeId('sajat_map');
var html = "<table>" +
"<tr><td>Név:</td> <td><input type='text' id='name'/> </td> </tr>" +
"<tr><td>Hely:</td> <td><input type='text' id='address'/></td> </tr>" +
"<tr><td></td><td><input id='save' type='button' value='Mentés' onclick='saveData()'/></td></tr>";
infowindow = new google.maps.InfoWindow({
content: html
});
google.maps.event.addListener(map, "click", function(event) {
marker = new google.maps.Marker({
position: event.latLng,
map: map
});
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map, marker);
});
});
downloadUrl("phpsqlajax_genxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
marker = new google.maps.Marker({
map: map,
icon:image,
position: point
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function saveData() {
var name = escape(document.getElementById("name").value);
var address = escape(document.getElementById("address").value);
var latlng = marker.getPosition();
var url = "phpsqlinfo_addrow.php?name=" + name + "&address=" + address +
"&lat=" + latlng.lat() + "&lng=" + latlng.lng();
alert('Successfully saved!');
downloadUrl(url, function(data, responseCode) {
if (responseCode == 200 && data.length <= 1) {
infowindow.close();
document.getElementById("message").innerHTML = "Location added.";
}
});
}
function codeAddress() {
var address = document.getElementById("kereso").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Sikertelen. A probléma oka: " + status);
}
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}