当我将代码从谷歌地图v2迁移到v3时,我想知道当我可以使警报符号写入调试信息时,如何访问my map上的点击点我想要访问点击的点图:
google.maps.event.addListener(map, "click", gAdd);
我的相关代码:
function gAdd(overlay, latlng) {
alert("test"+overlay);
if (latlng != null) {
alert("test");//address = latlng;
try {
alert("test");//geocoder.getLocations(latlng, gDisplay);
} catch (e) {}
}
}
function initialize() {
document.upload.lat.value = geoip_latitude();
document.upload.lng.value = geoip_longitude();
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(geoip_latitude(), geoip_longitude());
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: 'roadmap'
}
map = new google.maps.Map(document.getElementById("gmap"), myOptions);
google.maps.event.addListener(map, "click", gAdd);
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById("message").innerHTML = results[5].formatted_address;
} else {
}
});
if (navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var latlng = initialLocation
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent('<a href="/li?lat=' + latlng.lat() + '&lon=' + latlng.lng() + '">' + results[1].formatted_address + '</a>');
infowindow.open(map, marker);
document.upload.lat.value = latlng.lat();
document.upload.lng.value = latlng.lng();
document.upload.place.value = results[5].formatted_address
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}, function () {
handleNoGeolocation(browserSupportFlag);
});
} else if (google.gears) {
// Try Google Gears Geolocation
browserSupportFlag = true;
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.latitude, position.longitude);
//contentString = "Location found using Google Gears";
var latlng = initialLocation
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
//map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent('<a href="/li?lat=' + latlng.lat() + '&lon=' + latlng.lng() + '">' + results[1].formatted_address + '</a>');
infowindow.open(map, marker);
document.upload.lat.value = latlng.lat();
document.upload.lng.value = latlng.lng();
//alert('6 '+results[6].formatted_address );
document.upload.place.value = results[5].formatted_address;
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}, function () {
handleNoGeolocation(browserSupportFlag);
});
} else {
// Browser doesn't support Geolocation
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
这曾经与google maps v2一起使用,现在我想将它迁移到v3。
答案 0 :(得分:2)
点击处理程序在V3中获取一个事件对象,坐标位于the latLng
property:
function gAdd(ev) {
var latlng = ev.latLng;
//...
}