我相當新的JavaScript,我一直堅持這個問題在過去的幾個小時。一個對象,多個onclick事件
我有一個地圖對象,我附加了一個事件,所以如果用戶點擊地圖我想獲得鼠標按鈕的座標。然後我希望用戶點擊地圖上的另一個點來獲得第二對座標,然後我想在這兩點之間劃一條線。到目前爲止,我可以通過鼠標點擊第一對座標並通過聽用戶按下按鈕來獲得第二對座標。不過,我想用鼠標點擊兩下。點擊位置1獲取座標1,點擊位置2獲取座標2.每次我有兩個onclick事件,但是他們都立即開始,我只是在地圖上得到一個點。我正在使用Bing地圖API。
這裏是我的代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript"
src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?
v=6.2"></script>
<div><script type="text/javascript">
var map = null;
var pinid = 1;
var pixel = null;
var location = null;
var location2 = null;
var pixel2 = null;
var point1 = null;
var point2 = null;
var count = 0;
function doSomething(e) {
if (!e) var e = window.event;
e.cancelBubble = true;
if(e.stopPropagation) e.stopPropagation();
PixelClick1();
}
function GetMap()
{
map = new VEMap('myMap');
map.LoadMap();
map.SetZoomLevel(10);
map.AttachEvent("onclick", PixelClick1);
//onclick="doSomething();PixelClick1; return false"
}
function onClick(){
count++;
}
function AddPolyline()
{
var shape = new VEShape(VEShapeType.Polyline, [location2,
location
]);
map.AddShape(shape);
shape.HideIcon();
point1 = new VEShape(VEShapeType.Polyline,[location,location]);
point2 = new VEShape(VEShapeType.Polyline, [location2, location2]);
point1.SetTitle('Point1 Location:');
point2.SetTitle('Point2 Location:');
point1.SetDescription('(Latitude: ' + location.Latitude + ',
Longitude: ' + location.Longitude + ')');
point2.SetDescription('(Latitude: ' + location2.Latitude + ',
Longitude: ' + location2.Longitude + ')');
point1.HideIcon();
point2.HideIcon();
map.AddShape(point1);
map.AddShape(point2);
}
function PixelClick1(e){
e.cancelBubble = true;
var x = e.mapX;
var y = e.mapY;
pixel = new VEPixel(x,y);
location = map.PixelToLatLong(pixel);
wait(e);
return false;
}
function PixelClick2(e){
var x = e.mapX;
var y = e.mapY;
pixel2 = new VEPixel(x,y);
location2 = map.PixelToLatLong(pixel2);
AddPolyline();
}
function ShowPushPin(){
point1.ShowIcon();
point2.ShowIcon();
}
function HidePushPin(){
point1.HideIcon();
point2.HideIcon();
}
function wait(){
map.AttachEvent("onkeydown",PixelClick2);
}
</script></div>
</head>
<body onload="GetMap();">
<div id='myMap' style="position:relative; width:400px;
height:400px;"></div>
<div><a href='#' onclick='ShowPushPin();'>Show Pushpins</a></div>
<div><a href='#' onclick='HidePushPin();'>Hide Pushpins</a></div>
</body>
</html>
請顯示您的代碼(相關位)。也許你還在http://jsfiddle.net/ – Tomalak