Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Convert PHP to VB.NET

Posted on 2008-11-10
11
Medium Priority
?
1,652 Views
Last Modified: 2013-12-13
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 
0
Comment
Question by:azyet24
  • 6
  • 5
11 Comments
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 22928864
Hi azyet24,
Try this google map that converted in .NET:
http://www.codeproject.com/KB/custom-controls/LatLaysFlat-Part1.aspx
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 22928919
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
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 22928926
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:azyet24
ID: 22931121
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
 

Author Comment

by:azyet24
ID: 22931331
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
 

Author Comment

by:azyet24
ID: 22931338
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
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 22936696
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
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 2000 total points
ID: 22936734
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
 

Author Comment

by:azyet24
ID: 22939768
This is perfect.  One question: If I wanted to also assign the Google Key at run time, how would I do that?
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 22946875
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
 

Author Closing Comment

by:azyet24
ID: 31515196
Thank you, Thank you, Thank you!!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question