stappert14
asked on
X/Y coordinate pinpointer on an image
I'm not sure if JavaScript is the way to go on this but I'm trying to make pinpoints on a map using X/Y coordinates. I want to have a city map and then, using a simple text file or even a database, place pinpoints with a simple description based on X/Y coordinates, like the coordinates used for image maps. If this is at all possible, or even if you know a better language to ask for help in, please let me know. Thanks!
Here's a quick example without storing the information (you can use your own urls for the images):
<script>
var pinSrc="pin2.gif",xpos,ypo s,cnt=0;
function placePin(){
var pin=document.createElement ("div");
var img=document.createElement ("img");
img.src=pinSrc;
pin.appendChild(img);
cnt++;
pin.id="pin"+cnt;
pin.style.position="absolu te";
pin.style.left=xpos-pin.of fsetWidth/ 2;
pin.style.top=ypos-pin.off setHeight/ 2;
document.body.appendChild( pin);
}
function getMousePos(e){
if(document.all){xpos=wind ow.event.x +document. body.scrol lLeft;ypos =window.ev ent.y+docu ment.body. scrollTop; }
else{xpos=e.pageX;ypos=e.p ageY;}
}
document.onmousemove=getMo usePos;
</script>
<img src="map.jpg" name=map onclick="placePin();">
<script>
var pinSrc="pin2.gif",xpos,ypo
function placePin(){
var pin=document.createElement
var img=document.createElement
img.src=pinSrc;
pin.appendChild(img);
cnt++;
pin.id="pin"+cnt;
pin.style.position="absolu
pin.style.left=xpos-pin.of
pin.style.top=ypos-pin.off
document.body.appendChild(
}
function getMousePos(e){
if(document.all){xpos=wind
else{xpos=e.pageX;ypos=e.p
}
document.onmousemove=getMo
</script>
<img src="map.jpg" name=map onclick="placePin();">
That one didn't centre the pin properly, this one is better:
<script>
var pinSrc="pin.gif",xpos,ypos ,cnt=0;
function placePin(){
var pin=document.createElement ("div");
var img=document.createElement ("img");
img.src=pinSrc;
pin.appendChild(img);
cnt++;
pin.id="pin"+cnt;
pin.style.position="absolu te";
document.body.appendChild( pin);
pin.style.left=xpos-pin.of fsetWidth/ 2;
pin.style.top=ypos-pin.off setHeight/ 2;
}
function getMousePos(e){
if(document.all){xpos=wind ow.event.x +document. body.scrol lLeft;ypos =window.ev ent.y+docu ment.body. scrollTop; }
else{xpos=e.pageX;ypos=e.p ageY;}
}
document.onmousemove=getMo usePos;
</script>
<img src="map.jpg" name=map onclick="placePin();">
<script>
var pinSrc="pin.gif",xpos,ypos
function placePin(){
var pin=document.createElement
var img=document.createElement
img.src=pinSrc;
pin.appendChild(img);
cnt++;
pin.id="pin"+cnt;
pin.style.position="absolu
document.body.appendChild(
pin.style.left=xpos-pin.of
pin.style.top=ypos-pin.off
}
function getMousePos(e){
if(document.all){xpos=wind
else{xpos=e.pageX;ypos=e.p
}
document.onmousemove=getMo
</script>
<img src="map.jpg" name=map onclick="placePin();">
<html>
<head>
<title></title>
<script type="text/javascript">
<!--
var xCoord = function(evt){
if(evt.x){ return evt.x;}
if(evt.pageX){ return evt.pageX;}
}
var yCoord = function(evt){
if(evt.y){ return evt.y;}
if(evt.pageY){ return evt.pageY;}
}
document.onmousemove=funct ion(){
document.forms[0].x.value = xCoord(event);
document.forms[0].y.value = yCoord(event);
}
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="">
X<input type="text" name="x">
Y<input type="text" name="y">
</form>
</body>
</html>
<head>
<title></title>
<script type="text/javascript">
<!--
var xCoord = function(evt){
if(evt.x){ return evt.x;}
if(evt.pageX){ return evt.pageX;}
}
var yCoord = function(evt){
if(evt.y){ return evt.y;}
if(evt.pageY){ return evt.pageY;}
}
document.onmousemove=funct
document.forms[0].x.value = xCoord(event);
document.forms[0].y.value = yCoord(event);
}
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="">
X<input type="text" name="x">
Y<input type="text" name="y">
</form>
</body>
</html>
ASKER
I don't want a point and click type of interface. I'd like to have the image load the information from an external source and display the pinpoints. RedLava seems to be on the right track. I'm using a web host that supports pretty much all types of server-side scripting (ASP, PHP, etc.). I am relatively familiar with using dynamic text in Flash by loading it from a text file, is it possible to dynamically control it's placement also?
Here is an example using ASP.
The map.asp page:
-------------------------- ---------- ---------- --
<img src="map.jpg" name=map>
<script language=javascript>
var pinLoc=new Array();
<%
set fso = Server.CreateObject("scrip ting.FileS ystemObjec t")
set file = fso.opentextfile(server.ma ppath("db/ pinLoc.txt "),1,false )
do while not file.AtEndOfStream
response.write(" pinLoc[pinLoc.length]='"+f ile.readli ne+"';"+ch r(10))
loop
file.close
set file=nothing
set fso=nothing
%>
var pinSrc="pin.gif",pinName=" marker";
var ol=0,ot=0;
var obj=document.images["map"] ;
while(obj.offsetParent){
ol+=obj.offsetLeft;
ot+=obj.offsetTop;
obj=obj.offsetParent;
}
for(i=0;i<pinLoc.length;i+ +){
placePin(pinLoc[i].split(' ,')[0],pin Loc[i].spl it(',')[1] );}
function placePin(x,y){
var pin=document.createElement ("img");
pin.src=pinSrc;
pin.name=pinName;
pin.id=pinName;
pin.style.position="absolu te";
document.body.appendChild( pin);
pin.style.left=x-pin.offse tWidth/2+o l;
pin.style.top=y-pin.offset Height/2+o t;
}
</script>
-------------------------- ---------- ---------- --
The pinLoc.txt file:
-------------------------- ---------- ---------- --
20,20
40,40
60,60
80,80
100,100
130,130
150,150
-------------------------- ---------- ---------- --
If you would like to see an example, go to this url:
http://www34.brinkster.com/lilpuffball/map.asp
The text file is here:
http://www34.brinkster.com/lilpuffball/db/pinLoc.txt
The map.asp page:
--------------------------
<img src="map.jpg" name=map>
<script language=javascript>
var pinLoc=new Array();
<%
set fso = Server.CreateObject("scrip
set file = fso.opentextfile(server.ma
do while not file.AtEndOfStream
response.write(" pinLoc[pinLoc.length]='"+f
loop
file.close
set file=nothing
set fso=nothing
%>
var pinSrc="pin.gif",pinName="
var ol=0,ot=0;
var obj=document.images["map"]
while(obj.offsetParent){
ol+=obj.offsetLeft;
ot+=obj.offsetTop;
obj=obj.offsetParent;
}
for(i=0;i<pinLoc.length;i+
placePin(pinLoc[i].split('
function placePin(x,y){
var pin=document.createElement
pin.src=pinSrc;
pin.name=pinName;
pin.id=pinName;
pin.style.position="absolu
document.body.appendChild(
pin.style.left=x-pin.offse
pin.style.top=y-pin.offset
}
</script>
--------------------------
The pinLoc.txt file:
--------------------------
20,20
40,40
60,60
80,80
100,100
130,130
150,150
--------------------------
If you would like to see an example, go to this url:
http://www34.brinkster.com/lilpuffball/map.asp
The text file is here:
http://www34.brinkster.com/lilpuffball/db/pinLoc.txt
ASKER
lil puffball-
Looks great!! I haven't had a chance to try it out but from your examples I think it's just what I need. Is it at all possible to add a link for the pins and possibly even a short description? The link would be most preferable.
Looks great!! I haven't had a chance to try it out but from your examples I think it's just what I need. Is it at all possible to add a link for the pins and possibly even a short description? The link would be most preferable.
No problem. :) I've updated the pages, here is the new code:
map.asp
-------------------------- ---------- -
<img src="map.jpg" name=map>
<script language=javascript>
var pinLoc=new Array();
<%
set fso = Server.CreateObject("scrip ting.FileS ystemObjec t")
set file = fso.opentextfile(server.ma ppath("db/ pinLoc.txt "),1,false )
do while not file.AtEndOfStream
response.write(" pinLoc[pinLoc.length]='"+f ile.readli ne+"';"+ch r(10))
loop
file.close
set file=nothing
set fso=nothing
%>
var pinSrc="pin.gif",pinName=" marker";
var ol=0,ot=0;
var obj=document.images["map"] ;
while(obj.offsetParent){
ol+=obj.offsetLeft;
ot+=obj.offsetTop;
obj=obj.offsetParent;
}
for(i=0;i<pinLoc.length;i+ +){
placePin(pinLoc[i].split(' ,'));}
function placePin(data){
var pin=document.createElement ("img");
pin.src=pinSrc;
pin.name=pinName;
pin.id=pinName;
pin.onclick=function(){loc ation.href =data[2];}
pin.title=data[3];
pin.style.position="absolu te";
document.body.appendChild( pin);
pin.style.left=data[0]-pin .offsetWid th/2+ol;
pin.style.top=data[1]-pin. offsetHeig ht/2+ot;
}
</script>
-------------------------- ---------- -
pinLoc.txt
-------------------------- ---------- -
20,20,link1.htm,Descr. 1
40,40,link2.htm,Descr. 2
60,60,link3.htm,Descr. 3
80,80,link4.htm,Descr. 4
100,100,link5.htm,Descr. 5
130,130,link6.htm,Descr. 6
150,150,link7.htm,Descr. 7
-------------------------- ---------- -
map.asp
--------------------------
<img src="map.jpg" name=map>
<script language=javascript>
var pinLoc=new Array();
<%
set fso = Server.CreateObject("scrip
set file = fso.opentextfile(server.ma
do while not file.AtEndOfStream
response.write(" pinLoc[pinLoc.length]='"+f
loop
file.close
set file=nothing
set fso=nothing
%>
var pinSrc="pin.gif",pinName="
var ol=0,ot=0;
var obj=document.images["map"]
while(obj.offsetParent){
ol+=obj.offsetLeft;
ot+=obj.offsetTop;
obj=obj.offsetParent;
}
for(i=0;i<pinLoc.length;i+
placePin(pinLoc[i].split('
function placePin(data){
var pin=document.createElement
pin.src=pinSrc;
pin.name=pinName;
pin.id=pinName;
pin.onclick=function(){loc
pin.title=data[3];
pin.style.position="absolu
document.body.appendChild(
pin.style.left=data[0]-pin
pin.style.top=data[1]-pin.
}
</script>
--------------------------
pinLoc.txt
--------------------------
20,20,link1.htm,Descr. 1
40,40,link2.htm,Descr. 2
60,60,link3.htm,Descr. 3
80,80,link4.htm,Descr. 4
100,100,link5.htm,Descr. 5
130,130,link6.htm,Descr. 6
150,150,link7.htm,Descr. 7
--------------------------
The problem is that you can't have commas in the description. To fix this, you can use another separator, like the pipe (|). Then just change these lines:
for(i=0;i<pinLoc.length;i+ +){
placePin(pinLoc[i].split(' ,'));}
to this:
for(i=0;i<pinLoc.length;i+ +){
placePin(pinLoc[i].split(' |'));}
for(i=0;i<pinLoc.length;i+
placePin(pinLoc[i].split('
to this:
for(i=0;i<pinLoc.length;i+
placePin(pinLoc[i].split('
ASKER
Alright, I know I might be getting picky here but I don't know much about ASP coding. Can you get a 'hand' cursor over the pins so that you can tell it's a link??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
lil puffball, you definitely earned the points on this one..I even upped the points to 500. I really appreciate it!! The point of all this is that I have a database of different property sites that are available, and I wanted to be able to dynamically place pinpoints on a map for each property location. By doing a little cheating I managed to convert your short ASP snippet to PHP code that reads from my database. Here's what it looks like:
<?php
$row_count = 0;
while ($row = mysql_fetch_array($result) ) {
$ID = $row["ID"];
$name = $row["Name"];
$xCord = $row["xCord"];
$yCord = $row["yCord"];
echo "pinLoc[pinLoc.length]='$x Cord,$yCor d,$ID.htm, $name';
";
$row_count++;
}
?>
Once again, thank you very much for your help, you're a life saver!!
<?php
$row_count = 0;
while ($row = mysql_fetch_array($result)
$ID = $row["ID"];
$name = $row["Name"];
$xCord = $row["xCord"];
$yCord = $row["yCord"];
echo "pinLoc[pinLoc.length]='$x
";
$row_count++;
}
?>
Once again, thank you very much for your help, you're a life saver!!
Glad to help. Thanks for the A and all the points! :D
This is definatly possible with a combination of JavaScript and DHTML with some server-side scripting to read in the data.
What type of server-side scripting can you use? PHP, ASP etc.
Once you have the coordinates loaded in you can then use JavaScript to position layers containing the description text.
The points can also be used to create an image map but how the map is constructed from the points e.g. which point connects to which may require an additional list to create the shapes.
Another option would be Flash. This would make it a bit more graphical but the image map problem may cause an issue again.
RedLava