Google Maps in HTML, ASP - NET, PHP, JSP, Etc. With Ease - CodeProject
Google Maps in HTML, ASP - NET, PHP, JSP, Etc. With Ease - CodeProject
articles quick answers discussions features Search for articles, questions, tips
community help
The article will guide you with complete knowledge of how to add a Google map in your webpage
with knowledge of JavaScript, use of Geocoder, use of InfoWindow, use of Marker, Tabbed Markers,
Maximising marker, creating context menu, streetview in your map
Download source - 47 KB
Table of Contents
Introduction
Background
Sample Examples
Getting Started
Create Polylines on Custom Map
Geocode Address for Lat/lon
Create Context Menu
Tabbed Infowindow With Maximize Capability
Trap Client Events for Data
Markermanager and Sidebar
Drawing Custom Markers
Invoke AJAX From GMAP & Gpolygon
Streetview With Maximize & Close Buttons
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 1/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
Introduction
This article will guide you with a bit of knowledge about the use of JavaScript, and more precisely
will enable you to use Google Maps in your webpage with ease. Google brought web services that
anyone can connect to and use their services without any cost. Well, there is some restriction for
the use of gmap. They have two versions of maps, one for standard purpose which will come for
free, and another one is Google map Enterprise Solution.
Well, simply if you want to add a Google map to your website and which can be seen from
anywhere outside, means can be accessible to just using a link, you don't need to pay Google for
using their API. But if you want to restrict access to maps behind a valid login, then you need to
pay Google. I am using a simple HTML page for adding up gmap. And later on, I will use others
also to make the gmap look very lively.
The only thing that you need in case of using gmap to your application is an Google account. You
can sign up for an account for free. Just go here.
You could sign up for an account for free, and then you can use Google maps to your website by
clicking on "Create an account now". After you create an account, just use your userid and
password to login when required.
Background
For using Google Maps in your website, the thing you need is a bit of knowledge of JavaScript,
means how to call a function from web controls. I will describe everything, but you need to catch
up all those.
Sample Examples
I am creating an HTML page for implementing this project. You can use any projects to implement
maps dynamically using any server side languages.
//
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 2/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>My Locations</title>
<script src="http://maps.google.com/maps?file=api&v=2&
key=ABQIAAAAcl" type="text/javascript"></script>
<script type="text/javascript">
function load()
{
if (GBrowserIsCompatible()) {
var point;
var map=new GMap2(document.getElementById("map"));
map.addControl(new GOverviewMapControl());
map.enableDoubleClickZoom();
map.enableScrollWheelZoom();
map.addControl(new GMapTypeControl());
map.addControl(new GSmallMapControl());
var address='<img src="myimage.gif" width=150 height=40/><br/>' +
<font size="2" face="Arial"><b>INDIA</b><br/><br/>Home.<br/>' +
New York City<br/><br/>America<br/>Ph.: 23743823</font>';
var marker = new GMarker(point);
map.setCenter(point,17);
map.addOverlay(marker);
map.setMapType(G_HYBRID_MAP);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(address);});
marker.openInfoWindowHtml(address);
}
}
</script>
<body onload="load();" onunload="GUnload()" style="
background-color:Transparent">
<div id="map" style="width: 900px; height: 500px"></div>
</body>
</html>
Coming to the code, first in your webpage, you need to add one <div> where we are going to load
the Google map. I have taken a div similar to that and named it as "map". The width and height
will specify how much the size of the page will be.
JavaScript
<div id="map" style="width: 900px; height: 500px"></div>
Now, I have called a JavaScript function when body loads, thus, in my sample application, as soon
as the page comes to your browser, you will see a gmap loaded. So I have created a JavaScript
load function which will be called as soon as I open the webpage. Now during the page load, we
have added a dynamic JavaScript file which will be dependent on webhost. The line...
JavaScript
<script src="http://maps.google.com/maps?file=api&v=2&
key=ABQIAAAAcl" type="text/javascript"></script>
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 3/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
...adds up a file from Google. This is essential, because that file will create the entire map for you.
Just one thing that you need is to generate an API KEY for your web host. Be it local host, use
http://localhost to it. Just click below and get the key.
You will get a key from here, just replace the key in the key attribute of the JavaScript line.
After you do that, I have used GBrowserIsCompatible(), it's a function which will return true only
if your browser is compatible with gmap. The next line will load the gmap control to the div
control. GMap2 function takes the control to load and returns a map object which can be referenced
later on. Next, map.addControl(new GOverviewMapControl()); will add a new control to the map,
called GOverviewMapControl.
It will be an overview of the map just in the right hand bottom corner to the map.
I can add more controls through addControl Function. I have used GSmallMapControl().
as new map type control in a similar way. There are mainly 3 types
of map, they are Normal, Satellite and Hybrid. Those options could be found on your map.
After that, I have placed a marker on my application. Markers are images that can be placed over
the map to point a location. To place a marker, you need a latitude and longitude which should be
passed throught GLatLng() function to get a valid point. I have also made an HTML that could be
shown to the map. I have let a var address to store the HTML. Later on, I have created a marker
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 4/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
into the map using those latitude and longitude. The GMarker will create one marker and
addOverlay() will add the marker in the form.
The GEvent.addListener will add one listener to the map through which the map respond. I have
added a click event listener to my map, so that when one clicks on the map, it will show the
InfoWindow of the marker. openInfoWindowHtml() will open the info window HTML for me. That's
all. You have created a simple map and added to your HTML page.
JavaScript Shrink ▲
In the above example, I have taken 3 points just after creation of markers and drawn polyline over
those points.
GPolyline() will create a polyline object using an array of points, with a specific color, here I have
taken the color using hexadecimal code and the width, which is 10 here. After that, I have used
addOverlay function to add the polyline to our actual map.
Remember to add the vml namespace in case of using polylines. We also need to use strict DTD
standard to show polylines on the map.
JavaScript
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:v="urn:schemas-microsoft-com:vml">
Otherwise, the polyline will not be shown to your map. Take a look at the snapshot:
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 6/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
JavaScript Shrink ▲
</script>
</head>
<body onload="load()" onunload="GUnload()">
</body>
</html>
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 7/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
In the above example, I have added another function that will enable us to call any address and the
address will be replaced with a point in terms of latitude and longitude. The GClientGeocoder()
will return a geocode object to the client browser which could be later used to get a point from any
text address. Google has approximated this search will take 1.73 seconds for each geocode call. So
it's not that much slow. Now for searching an address, I have created a button and a textbox,
which will be used to get address from the user. The showaddress function will take one address
from the user and create a marker just into that location, and open the infowindow of that marker.
If no points are found, it returns an alert. So on clicking the button, it will call the showaddress
function, and it will show the location.
JavaScript Shrink ▲
map.addControl(new GOverviewMapControl());
map.enableDoubleClickZoom();
map.enableScrollWheelZoom();
map.addControl(new GMapTypeControl());
map.addControl(new GLargeMapControl());
createContextMenu(map);
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 8/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
var address='<font size="2" face="Arial"><b>INDIA</b><br/>
<br/>XYZ Inc.<br/>New York City <br/>
America<br/>Ph.: 343254543</font>';
point = new GLatLng(22.592057,88.421815);
}
}
function createContextMenu(map)
{
contextmenu = document.createElement("div");
contextmenu.style.visibility="hidden";
contextmenu.style.background="#ffffff";
contextmenu.style.border="1px solid #8888FF";
map.getContainer().appendChild(contextmenu);
GEvent.addListener(map,"singlerightclick",function(pixel,tile)
{
clickedPixel = pixel;
var x=pixel.x;
var y=pixel.y;
if (x > map.getSize().width - 120)
{
x = map.getSize().width - 120
}
if (y > map.getSize().height - 100)
{
y = map.getSize().height - 100
}
var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));
pos.apply(contextmenu);
contextmenu.style.visibility = "visible";
});
GEvent.addListener(map, "click", function()
{
contextmenu.style.visibility="hidden";
});
}
function zoomIn()
{
map.zoomIn();
contextmenu.style.visibility="hidden";
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 9/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
}
function zoomOut()
{
map.zoomOut();
contextmenu.style.visibility="hidden";
}
function zoomInHere()
{
var point = map.fromContainerPixelToLatLng(clickedPixel)
map.zoomIn(point,true);
contextmenu.style.visibility="hidden";
}
function zoomOutHere()
{
var point = map.fromContainerPixelToLatLng(clickedPixel)
map.setCenter(point,map.getZoom()-1);
contextmenu.style.visibility="hidden";
}
function centreMapHere()
{
var point = map.fromContainerPixelToLatLng(clickedPixel)
map.setCenter(point);
contextmenu.style.visibility="hidden";
}
In this example, I have added a custom div control when the user right clicks anywhere in the map.
The event that should be registered when user right clicks the map is
GEvent.addListener(map,"singlerightclick",function(pixel,tile). I have created on the
same point where the right click occurs, by getting the points from pixel. The div have some
function calls when option is clicked. Those calls the map.zoomIn, ZoomOut functions. The container
is set to hidden when the option works just to eliminate the custom div control from the map.
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 10/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
JavaScript Shrink ▲
Well, you can see the line GEvent.addListener where instead of calling openInfoWindow, we are
calling openInfoWindowTabsHtml. This function will enable us to create a tabbed window. The
arguments are array of Tabs that could be found from GInfoWindowTab, which takes two
arguments, the first one is header, and the second is the content in HTML. The second argument of
openInfoWindowTabsHtml is options, where I have used maxUrl option, which will create a
maximize button on the window. I can use this option in normal markers too. Now let's look at the
snapshots:
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 11/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
1. Basic and
2. Advanced
When you click on the + button just beside the Close button, It will open an HTML window within
the gmap. See the snapshot:
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 12/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
JavaScript Shrink ▲
<script type="text/javascript">
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl (new GSmallMapControl());
map.addControl(new GMapTypeControl());
var center = new GLatLng(-22.98699983834975, -43.210344314575195);
map.setCenter(center, 11);
map.setMapType(G_SATELLITE_MAP);
geocoder = new GClientGeocoder();
function showAddress(address) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
if (geocoder) {
geocoder.getLatLng (
address,
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 13/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
function(point) {
if (!point) {
alert(address + " city not found !");
}
else {
document.getElementById("lat").value = point.lat();
document.getElementById("lng").value = point.lng();
map.clearOverlays()
map.setCenter(point, 14);
var marker = new GMarker(point, {draggable: true});
map.addOverlay(marker);
In this example, when the use drags the map, it will automatically place coordinates to the form
text boxes called lat and lng. To do this, I have made an overlay marker, and then retrieved the
point using getPoint() function.
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 14/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
This is a simple example... just a demo. I have also included the geocoder, so that one can also
search for a location.
The Marker Manager of Google is used to manage large number of markers. See the example
below:
JavaScript Shrink ▲
<!-- you can use tables or divs for the overall layout -->
<table border=1>
<tr>
<td>
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 15/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
<div id="map" style="width: 550px; height: 450px"></div>
</td>
<td width = 150 valign="top" style="text-decoration: underline; color: #4444ff;">
<div id="side_bar"></div>
</td>
</tr>
</table>
<script type="text/javascript">
//<![CDATA[
if (GBrowserIsCompatible()) {
// this variable will collect the html which will eventually be placed
// in the side_bar
var side_bar_html = "";
// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there
var gmarkers = [];
var i = 0;
var lastlinkid;
// This function picks up the click and opens the corresponding info window
function Linkclicked(i) {
GEvent.trigger(gmarkers[i], "click");
}
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 16/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
GEvent.addListener(map,"infowindowclose", function() {
document.getElementById(lastlinkid).style.background="#ffffff";
});
mm.addMarkers(gmarkers,0,17);
mm.refresh();
else {
alert("Sorry, the Google Maps API is not compatible with this browser");
}
//]]>
</script>
</body>
</html>
Here, I have placed one marker manager and an array. I have put all the markers to the
markermanager so that it can be refreshed, deleted, or added later. The links are created using the
array and is properly mapped with correct marker. Check out the link:
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 17/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
To use Tom Morgans Marker, just download this JavaScript from here: Click here.
JavaScript Shrink ▲
<script type="text/javascript">
//<![CDATA[
if (GBrowserIsCompatible()) {
var openbubble=true;
var agent = navigator.userAgent.toLowerCase();
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 18/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
// For IE We need to Do this
if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){
var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(
src='gmarker.png', sizingMethod='crop');";
icon = '<div style="height:34px; width:20px; ' +loader+ '" ></div>'
}
var n=0;
function createTLabel(point,html) {
var label = new TLabel();
label.id = 'Label '+n;
label.anchorLatLng = point;
label.anchorPoint = 'bottomCenter';
label.content = html;
label.markerOffset = new GSize(-1,-5);
map.addTLabel(label);
var obj=document.getElementById(label.id);
GEvent.addDomListener(obj, "click", function() {
//map.openInfoWindowHtml(point, html, {pixelOffset: new GSize(0,-34)} );
});
n++;
}
else {
alert("Sorry, the Google Maps API is not compatible with this browser");
}
function openInfo(d)
{
var obj = document.getElementById(d);
if(openbubble==true)
{
obj.style.display="block";
openbubble=false;
}
else
{
obj.style.display="none";
openbubble=true;
}
}
//]]>
</script>
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 19/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
</body>
</html>
You can use Gmarker array to store the labels for future reference, and also you can add one event
to the map so that each open marker hides through a loop.
First of all, look at the example and code below. I am explaining them after that.
JavaScript Shrink ▲
<script src="http://maps.google.com/maps?file=api&v=2&
key=ABQIAAAAclK0B2lXQwV5lPy1rL"
type="text/javascript"></script>
<script type="text/javascript">
function load()
{
if (GBrowserIsCompatible())
{
var point;
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(42.16,-100.72),4);
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 20/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
for (var a = 0; a < states.length; a++)
{
// get any state attributes
var label = states[a].getAttribute("name");
var colour = states[a].getAttribute("colour");
// read each point on that line
var points = states[a].getElementsByTagName("point");
var stateoptions = document.getElementById("xmlStates");
var optn = document.createElement("OPTION");
optn.text = states[a].getAttribute("name");
optn.value = states[a].getAttribute("name");
stateoptions.options.add(optn);
}
}
}
request.send(null);
}
else {
alert("Sorry, the Google Maps API is not compatible with this browser");
}
}
function loadselectedpoint()
{
if(xmlDoc==null){alert('null');return;}
var state = document.getElementById("xmlStates").value;
var states = xmlDoc.documentElement.getElementsByTagName("state");
//]]>
</script>
</head>
<body onload="load();" onunload="GUnload()" style="background-color: Transparent">
States :
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 21/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
<select id="xmlStates" onchange="loadselectedpoint()">
</select>
<br /><br />
<div id="map" style="width: 900px; height: 500px">
</div>
</body>
</html>
In the above code, I have made one Select box, where all the States load, upon selecting one
state, it will display the entire boundary of the state. Please take a look at the demo below:
1. GXmlHttp: This class is the primary AJAX calling class which creates an XMLHttpRequest Object
and places Asynchronous server calls. I have used this class to load the points from an
external XML File which is asynchronously called and downloaded from the server. I have
used this file to load the markings and also to load the Select Control. The
OnReadyStateChanged is an event for any XMLHttpRequest object that is triggered when
state is changed to the object. The Readystate evaluates to 4 when the last call made is
successful, and the response is transmitted to the responseText property.
2. GXml: It's a class provided by Google that enables you to parse XML in your application. I have
used this class to manage the XML data that comes through AJAX calls.
3. GPolygon: This class is used to make an overlay to the map. The constructor takes 7
arguments. The first argument is for an array of points, which are the GLatlng combinations.
The second argument is the Color for Boundaries, third for weight of border, fourth is color of
border, fifth is the opacity of the border, sixth is fill color, and seventh is the opacity of the fill
color. You can also produce click event for the Polygon.
Recently, a new addition is made to Google map which allows us to view the actual street view of
the Road. This is really interesting. It was there for quite a few months in maps.google.com and
finally it has been released to the API.
JavaScript
var street;
function initialize() {
var position = new GLatLng(42.345573,-71.098326);
var opt = { latlng:position,enableFullScreen: true };
street = new GStreetviewPanorama(document.getElementById("divp"), opt);
GEvent.addListener(divp, "error", handleNoFlash);
}
function handleNoFlash(errorCode) {
if (errorCode == FLASH_UNAVAILABLE) {
alert("Error: Flash doesn't appear to be supported by your browser");
else if (errorCode == NO_NEARBY_PANO) {
alert("Error : StreetView is Unavailable for current location");
}
return;
}
}
If you click on link in the marker window, it will open the Streetview which looks like:
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 23/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
If you click on the above pictures, you can see the online demonstration of the feature. Just go to
View-Source to find out the entire source code or you may download the source added with this
article as well.
Here, I have placed a marker with a link to show the StreetView of a selected point. You might also
use the right-click context menu to show the StreetViewPanorama of any point over the map.
Note: StreetViewPanorama will not load if the point is not over a Road where PANO data is
available. You might use:
JavaScript
var gstclient = new GStreetviewClient();
gstClient.getNearestPanoramaLatLng(new GLatLong(px,py), panoCallback));
function panoCallback(point){
//show panorama here.
}
Thus, you can make use of panoCallback which receives the nearest GLatLong point from the
point passed in.
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 24/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
Answer: Actually, the XML file is called using AJAX XMLHttpRequest Object to the server. I have
uploaded the file to my website. Please take a look at:
The data is taken from Census Bureau Data of US. If you need current data, you need to login here
and get the recent changes (of course if Govt officially modifies their site with recent changes).
Answer: US Zip Code data is also available from various sources. I have made a database on
Census Data based on Zip location so that one can easily use that data to create custom boundary
from the Example 9 I gave you.
Answer: To determine the distance between 2 geo points, you can use two approaches.
The first one is to get Actual Distance Between 2 geo points which could be calculated using Great
Circle Formula. I have created one SQL server Function which will give you some idea on how to
calculate this:
SQL Shrink ▲
-- =============================================================================
-- Author: <Author, Abhishek Sur, Codeproject.COM.>
-- Create date: <Create Date,03/24/2008>
-- Description: <Description,Returns Distance between two GEO Points
-- on Great Circle>
-- =============================================================================
BEGIN
DECLARE
@LONDIFF float,@a Bigint,@b float,@f float,@U1 float,@U2 float,@sinU1 float,
@cosU1 float,@sinU2 float,@cosU2 float,@lambda float,
@lambdaP float,@iterLimit Int,@sinLambda float,@cosLambda float,
@sinSigma float,@cosSigma float,@sigma float,@alpha float,
@cosSqAlpha float,@cos2SigmaM float,@C float,@uSq float,@CAPA float,
@CAPB float,@deltaSigma float,@s float
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 25/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
-- CONVERT TO RADIAN
set @FIRSTLAT = (@FIRSTLAT * 3.14159265358979)/180
set @FIRSTLON = (@FIRSTLON * 3.14159265358979)/180
set @SECONDLAT = (@SECONDLAT * 3.14159265358979)/180
set @SECONDLON = (@SECONDLON * 3.14159265358979)/180
set @a = 6378137
set @b = 6356752.3142
set @f = 1/298.257223563
if (@iterLimit=0)
return 'Not Valid Data' -- formula failed to converge
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 26/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
end
else
begin
set @retval = round(1000 * @dist,3)/1000
set @retval = round(@retval * 0.621371192,3)
return CAST(@retval as varchar(12)) + ' Miles'
End
return '0 m'
End
You can also use client end JavaScript to do this. Just get the logic from here. For formula
reference, you can follow this link.
Secondly, you can use GDirection functionality to get the actual walking distance between the two
points:
JavaScript Shrink ▲
function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("Geo Address Not Found.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("Server Error occurred.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("Missing Query.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)
alert("Address Unavailable.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("Api Key not supported. \n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("Bad Request.\n Error code: " + gdir.getStatus().code);
else alert("An unknown error occurred.");
}
function onGDirectionsLoad(){
document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
}
Answer: Are you bored of the same Red Marker Icon? If so, you can change the Icon of the marker
using GIcon.
JavaScript
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 27/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
But note that setImage will not update the Shadow property of GIcon.
Answer: Obviously, there is an option to generate maps with your custom overlay image. Let us
see the example:
JavaScript
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(40.740, -74.18), 12);
// ground overlay
var boundaries = new GLatLngBounds(new GLatLng(40.716216,-74.213393),
new GLatLng(40.765641,-74.139235));
var oldmap = new GGroundOverlay("http://imageserv.com/myimage.jpg", boundaries);
map.setUIToDefault();
map.addOverlay(oldmap);
}
}
Answer: I have shown you how to draw custom polygon shapes based on data using AJAX. You
can easily find database which provides information on US zip codes and draw shapes on the map.
I have uploaded an SQL server Database backup. You can find that here.
Download the file. Unzip it and restore to SQL Server. You will find the data in a table called
USZIPINFO. The schema of the table is:
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 28/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
You can use this database to get the US Zip information country wise.
7. What are the Limitations of the Use of Google Map in Commercial Sites?
Answer: As far as I know, Google Maps is a free service and you can use it freely in your
commercial web sites, provided the map should be available freely from anywhere. By this word, I
mean the map should instantly appear from your site by just pasting the proper link to the address
bar. The Map should not be hidden behind a login to your website authentication. If you are
doing something other than that, you can use Google Custom Enterprize map.
Points to Remember
The points that you should always remember:
1. If you have found a message like "API key is not correctly registered for that site,"
you should change the key element of the JavaScript tag to a valid key. You could get the key
from this page.
2. Never use Google maps for showing illegal content. This may be violation to rights.
3. For enterprise solutions, there may be a situation where you have to secure the map behind
login. In that case, you should pay Google for enterprise gmap control.
History
It was really a fun creating map examples for all of you. I tried to put most of the basics in this
article but there is a lot more to go. You can just copy and place code to a Notepad, generate your
API key, replace it in your JavaScript, and get your application to run.
Second Version: In this update, I have added another feature to show custom context menu. Just
check the last example for that.
Third Version: 27nd September, 2009: After getting tons of mails to my mailbox, I added one FAQ
section to describe the question that is asked the most.
Latest Version: 2nd December, 2009: Street View panorama example added.
References
For further reading, you might start from:
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 29/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
License
This article, along with any associated source code and files, is licensed under The Code Project
Open License (CPOL)
Written By
Abhishek Sur
President
India
Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn
a lot from it.
http://bit.ly/EXPERTCookBook
Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure
you always find him talking about technical stuffs.
Watch
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 30/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
Re: How to show addresses stored in Database? Abhishek Sur 28-Jun-16 1:50
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 31/32
20/1/24, 0:04 Google Maps in HTML, ASP.NET, PHP, JSP, etc. with Ease - CodeProject
Last Visit: 18-Jan-24 21:30 Last Update: 20-Jan-24 0:02 Refresh 1 2 3 4 5 6 Next ᐅ
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
https://www.codeproject.com/Articles/20590/Google-Maps-in-HTML-ASP-NET-PHP-JSP-etc-with-Ease 32/32