Convert PHP to VB.NET

I found a Dynamic Google Maps code written in PHP, but I program in VB.NET.    Here is the site that has the code:

http://www.map-server.com/googlemaps/tutorial_api2.html 
azyet24Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David H.H.LeeCommented:
Hi azyet24,
Try this google map that converted in .NET:
http://www.codeproject.com/KB/custom-controls/LatLaysFlat-Part1.aspx
0
David H.H.LeeCommented:
Hi azyet24,
If you want to convert PHP code above, try this PHP to asp.net convertor for easier conversion:
http://www.asp.net/downloads/archived/migration-assistants/php-to-aspnet/

However, i can explain explain and you can apply it into your existing project:

Connect to database and retrieve data:
------------------------------------------------------------
$link = mysql_connect("[database server]", "[username]", "[password]") or die("Could not connect: " . mysql_error());
mysql_selectdb("[database name]",$link) or die ("Can\'t use dbmapserver : " . mysql_error());

$result = mysql_query("SELECT * FROM wholocations",$link);

$result can be your datasource, eg: DataReader

Iterate datasource and bind the code into dynamic javascript:
---------------------------------------------------------------------------------
if (!$result)
{
echo "no results ";
}
while($row = mysql_fetch_array($result))
{
echo "var point = new GLatLng(" . $row['lat'] . "," . $row['lon'] . ");\n";
echo "var marker = createMarker(point, '" . addslashes($row['description']) . "');\n";
echo "map.addOverlay(marker);\n";
echo "\n";
}

Here the conversion in .NET
----------------------------------------
Dim result as datareader

While result .Read
Response.Write("var point = new GLatLng("  & result.GetValue("lat") & ","  & result.GetValue("lon") & ");")
Response.Write("var marker = createMarker(point, '"   & Replace(result.GetValue("description"),"'","''")  & "')")
Response.Write("map.addOverlay(marker);")
Response.Write("<br>")
end while



0
David H.H.LeeCommented:
Slightly changes:
Response.Write("var marker = createMarker(point, '"   & Replace(result.GetValue("description"),"'","''")  & "')")
Change to
Response.Write("var marker = createMarker(point, '"   & Replace(result.GetValue("description"),"'","\'")  & "')")
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

azyet24Author Commented:
Thank you for your reply.  Hopefully I can get to use it.  Right now I am struggling to get the marker to display.  I have the coordinates hard coded and it pulls up the map fine, but no marker.  Attached is the code.  Can you help?
<head runat="server">
    <title></title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=MyKey"
      type="text/javascript"></script>
    <script type="text/javascript">
 
     function load() {
         //<![CDATA[
         var map = new GMap2(document.getElementById("map"));
         map.addControl(new GLargeMapControl());
         map.addControl(new GMapTypeControl());
         map.addControl(new GScaleControl());
         map.setCenter(new GLatLng(33.378575, -86.834309), 15, G_NORMAL_MAP);
 
         // Creates a marker whose info window displays the given number
         function createMarker(point, number) {
             var marker = new GMarker(point);
             // Show this markers index in the info window when it is clicked
             var html = number;
             GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
             return marker;
         };
}
</script>
</head>
<body onload="load();">
    <form id="form1" runat="server">
    <div id="map" style="width: 500px; height:450px;"></div>
    </form>
</body>
</html>

Open in new window

0
azyet24Author Commented:
And the code behind.  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Response.Write("var point = new GLatLng(" & "33.378575" & "," & "-86.834309" & ");")
        Response.Write("var marker = createMarker(point, '" & "This is the Description" & "')")
        Response.Write("map.addOverlay(marker);")
        Response.Write("<br>")
    End Sub

Open in new window

0
azyet24Author Commented:
Also, I do not see where this will actually define the lat/long of the map dynamically.  These values are shown as static.  I have to be able to load the map lat/long, and the marker lat/long at run time.  
0
David H.H.LeeCommented:
Hi azyet24,

Please see the HTML version that posted below regarding how to create marker, i'll post you the ASP.NET version shortly.
<HEAD>
	<title></title>
	<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=MyKey" type="text/javascript"></script>
	<script type="text/javascript">
 
     function load() {
        var map = new GMap2(document.getElementById("map"));
         map.addControl(new GLargeMapControl());
         map.addControl(new GMapTypeControl());
         map.addControl(new GScaleControl());
         map.setCenter(new GLatLng(33.378575, -86.834309), 15, G_NORMAL_MAP);
        
         var point = new GLatLng(33.378575,-86.834309);
		 var marker = createMarker(point, "This is the Description");
		 map.addOverlay(marker);
		
		 //<![CDATA[
         // Creates a marker whose info window displays the given number
         function createMarker(point, number) {
             var marker = new GMarker(point);
             
             // Show this markers index in the info window when it is clicked
             var html = number;
             GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
             return marker;
         };
}
	</script>
</HEAD>
<body onload="load();">
	<form id="form1">
		<div id="map" style="WIDTH: 500px; HEIGHT: 450px"></div>
	</form>
</body>

Open in new window

0
David H.H.LeeCommented:
Hi azyet24,

Please see the converted ASP.NET version below. There are a lot of different way of converting the solution, however here are the one of the attempt.
ASPX page
-------------
<HEAD>
	<title></title>
	<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=MyKey" type="text/javascript"></script>
</HEAD>
<body onload="load();">
	<form id="form1" runat="server">
		<div id="map" style="WIDTH: 500px; HEIGHT: 450px"></div>
	</form>
</body>
 
Code Behind
---------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        LoadControls()
    End Sub
 
    Sub LoadControls()
        Dim strLat As String = "33.378575"
        Dim strLang As String = "-86.834309"
        Dim strMarkerDescription As String = "This is the Description"
 
        Dim s As New System.Text.StringBuilder
 
        s.Append("<script language='javascript'>" & vbCrLf)
        s.Append("function load() {" & vbCrLf)
        s.Append("var map = new GMap2(document.getElementById('map'));" & vbCrLf)
        s.Append("map.addControl(new GLargeMapControl());" & vbCrLf)
        s.Append("map.addControl(new GMapTypeControl());" & vbCrLf)
        s.Append("map.addControl(new GScaleControl());" & vbCrLf)
        s.Append("map.setCenter(new GLatLng(" & strLat & ", " & strLang & "), 15, G_NORMAL_MAP);" & vbCrLf)
        s.Append("var point = new GLatLng(" & strLat & "," & strLang & ");" & vbCrLf)
        s.Append("var marker = createMarker(point, '" & strMarkerDescription & "');" & vbCrLf)
        s.Append("map.addOverlay(marker);" & vbCrLf & vbCrLf)
        s.Append(vbCrLf & vbCrLf)
        s.Append("//<![CDATA[" & vbCrLf)
        s.Append("// Creates a marker whose info window displays the given number" & vbCrLf)
        s.Append("function createMarker(point, number) {" & vbCrLf)
        s.Append("var marker = new GMarker(point);" & vbCrLf & vbCrLf)
        s.Append("// Show this markers index in the info window when it is clicked" & vbCrLf)
        s.Append("var html = number;" & vbCrLf)
        s.Append("GEvent.addListener(marker, ""click"", function() { marker.openInfoWindowHtml(html); });" & vbCrLf)
        s.Append("return marker;" & vbCrLf)
        s.Append(" };" & vbCrLf)
        s.Append("}" & vbCrLf)
        s.Append("</script>")
 
        Page.RegisterClientScriptBlock(Guid.NewGuid.ToString, s.ToString)
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
azyet24Author Commented:
This is perfect.  One question: If I wanted to also assign the Google Key at run time, how would I do that?
0
David H.H.LeeCommented:
Hi azyet24,
You can load external JS source dynamically.
eg:
ASPX
------------------
<HEAD>
      <title></title>
</HEAD>

Code-Behind
------------------
    Dim jsGoogleMap as string = "http://maps.google.com/maps?file=api&v=2&key=MyKey" '<---amend your JS source here

    If (not Page.IsClientScriptBlockRegistered(jsGoogleMap))
        Dim strScript = "<script src='" + jsGoogleMap+ "' type='text/javascript'></script>"
        Page.RegisterClientScriptBlock(jsGoogleMap, strScript)
    End If
0
azyet24Author Commented:
Thank you, Thank you, Thank you!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.