Link to home
Create AccountLog in
Avatar of ChloesDad
ChloesDadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Adding markers to a google map from an asp web page

I have a web page that displays a google map. That buit works fine. I then want to read some coordinates from a database and add markers to the map.

The problem that I have is that the code to add markers to the map is Javascript and my web page is asp and I'm having trouble calling the javascript from the asp.


Javascript
<script language="javascript" type="text/javascript">   
  src="https://maps.googleapis.com/maps/api/js?sensor=false">     
</script> 

<script language="javascript" type="text/javascript">     
function initialize() {         
  var mapOptions = {           
	  center: new google.maps.LatLng(39.861667, -104.673056),           
		zoom: 5,           
		mapTypeId: google.maps.MapTypeId.ROADMAP         
	};         
	var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);       
}       

Function AddPoint(Pilot,Latitude,longitude) {

  var point = new GLatLng(latitude,longitude);
  map.addOverlay(new GMarker(point));
}


google.maps.event.addDomListener(window, 'load', initialize);     
</script>

</head>

Open in new window





asp

 Do While Not RS.EOF
    CALL AddPoint(PilotID,Latitude,Longitude)		 	
    RS.MoveNext	
  Loop	

Open in new window


I get the following error

Variable is undefined: 'AddPoint'
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

You can't call the javascript from the asp.  ASP runs on the server and sends the page to the browser where the javascript runs.  You can't directly call one from the other.

You can either use ASP to write an array the javascript can read or you can use AJAX to get it from the server.

Arrays: http://www.w3schools.com/js/js_datatypes.asp
AJAX: http://www.w3schools.com/ajax/default.asp
Avatar of ChloesDad

ASKER

Thanks, The array solution seems the most logical.

Where would I declare the array, in the Javascript, or in the asp, and how would it get passed from one to the other.
You would use ASP to get the data from the database, then write it out in javascript format in between the script tags where it will be used.  Here is a simple demo.  I'll leave it to you to figure the array of arrays.
<%response.buffer=true%>
<%
Set oConnection = Server.CreateObject("ADODB.Connection")
oConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\db\OldWList.mdb"

set rs = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * FROM websitelist WHERE ent_num = 2"
rs.Open sql, oConnection

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>JS array in ASP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<%Do while not(rs.eof)%>

var webs=new Array();
webs[0]="<%=rs("ent_num")%>";
webs[1]="<%=rs("SortName")%>";
webs[2]="<%=rs("Descript")%>";

<%
rs.movenext
loop

rs.Close()
%>

function show()
{
document.getElementById("ent_num").value = webs[0];
document.getElementById("SortName").value = webs[1];
document.getElementById("Descript").value = webs[2];
}


</script>
</head>
<body onload="show();">
<h1>JS array in ASP</h1>

<table width="300px" border="1" cellspacing="1" cellpadding="1">
 <tr>
   <td>ent_num</td>
    <td> 
<input type="text" name="ent_num" id="ent_num" value="">
    </td>
 </tr>
 <tr>
   <td>SortName</td>
   <td>
      <input type="text" name="SortName" id="SortName" value="">
    </td>
 </tr>
 <tr>
   <td>Descript</td>
   <td>
      <input type="text" name="Descript" id="Descript" value="">
    </td>
 </tr>
</table>

</body>
</html>

Open in new window

I have used this library to create json arrays with asp https://code.google.com/p/aspjson/
I have found a temporary solution by using my asp code to create the correct html code to display the map, so no Javascript is needed. I will try your solution later in the week when I get back home.
Somehow you have to use javascript???  You could simply response.write "<script> //make your json arrays </script>"

Another option is to use serverside js.  However, it runs very slow.  Side by side, server side vb runs faster then server side js in my own experiments.  But client side js will run the fastest.  

I generate javascript code from asp code many times.  Just remember the serverside code runs before the page is even drawn.  When you have, "response.write "<script> //make your json arrays </script>" what ends up taking place is simply pure js script when the page loads.  After the html loads then your javascript is run.
ASKER CERTIFIED SOLUTION
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer