Google Maps user control Introduction
Google Maps user control Features
- Ajax calls to retrieve server side data.
- Enables you to change pushpin positions on the fly. No need to refresh full page.
- Enables you to change pushpin icons and positions from asp.net backend code.
- Pushpin click and drag event support in asp.net code.
- Map click event support in asp.net code.
- Directions support. Allows you to draw route between multiple points.
- Polylines and Polygons support.
- Geocoding support, i.e. find latitude longitude from specific address and create pushpin on that location.
- Auto map boundary reset and zoom support to display all pushpins.
- Optimized to give you best performance. i.e. only those pushpin data will be retrieved from server that are changed.
- Visual Studio 2019 Community Edition or higher
- Internet Explorer or Firefox (Note: It may work on other browsers. I have tested on Internet Explorer and Firefox.)
Steps to integrate with ASP.Net application
- Download or clone source from Github link provided above.
- Open extracted folder as a website in Visual Studio and run it. When you run this website, you will be able to navigate few samples pages.
Once done, open you Solution in Visual Studio and go to Solution Explorer. Click on Show All files icon to show GoogleMapControl sub folder that we just copied in application root.
Try building your solution after including GoogleMapControl sub folder. Ensure there are no errors.
Adding Google Map control to your webpage
Drag “GoogleMapForASPNet.ascx” control to your web page(.aspx) file.
At this point you can run your application and you should be able to see a blank Google Map on your page as shown below. You will see an error as shown in below screenshot. This error is because of missing API key for google map.
Follow tutorial in below link to get your own Google Maps API key. Please note that you need to have a project in google cloud console with billing account to remove “For development purpose only” error. You can keep using development mode for testing with a blank API key.
Once you have your API key, go to Web.config file and add in below section.
<appSettings> <add key="GoogleAPIKey" value=""/> </appSettings>
After adding google API key, you should be able to see normal google map without any error.
Script Manager Error
In case you receive below error, then this means you already have a Script Manager in either Master page or page where you are trying to add control.
Only one instance of a ScriptManager can be added to the page.
To resolve above error, remove one instance of Script Manager from either GoogleMapControlForASPNet.ascx file or from your page.
How to use this control
Specify width and height for map. You can specify either in pixels or in percentage relative to it’s container.
GoogleMapForASPNet1.GoogleMapObject.Width = "800px"; GoogleMapForASPNet1.GoogleMapObject.Height = "600px";
Specify zoom level. Default is 3.
GoogleMapForASPNet1.GoogleMapObject.ZoomLevel = 14;
Specify Center Point for map. Map will be centered on this point.
GoogleMapForASPNet1.GoogleMapObject.CenterPoint = new GooglePoint("CenterPoint", 43.66619, -79.44268);
Add pushpins for map. This can be done by initializing GooglePoint type object. In constructor of GooglePoint, First argument is ID of this pushpin. It must be unique for each pin. Second and third arguments are latitude and longitude.
GoogleMapForASPNet1.GoogleMapObject.Points.Add(new GooglePoint("1", 43.65669, -79.45278));
Alternatively you can also do it like below,
GooglePoint GP = new GooglePoint(); GP.ID = "1"; GP.Latitude = 43.65669; GP.Longitude = -79.43270; GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP);
You can add as many pushpins as you wish. Now run website again and you should see pushpins on map.
Assigning custom icon to pushpins
You can assign your own icons with google map control. For that first copy your icons in some directory under root directory of your website. You can assign icon to a pushpin as below,
GP.IconImage = "icons/pushpin-blue.png";
Note that path to image is relative to root folder. You should have icons (or whichever) directory in root folder of your website.
You can add description of a pushpin which will pop up when user clicks a pushpin.
GP.InfoHTML = "This is Pushpin-1";
You can format InfoHTML property using standard HTML tags.
GP.InfoHTML = "This is Pushpin-1";
Up to this point, I have explained you basics of using Google Map control. Now let’s implement some advanced functionality. Let’s say we want to move pushpins when user do some action. For example when a user clicks on a button. For that, follow below steps.
Creating Interactive Map
You can create interactive map using Google Map control. You can move pushpins when user clicks on a button. Here is how you can do it.
Insert standard asp.net button on your web page. Write following code in click event of this button.
protected void Button1_Click(object sender, EventArgs e)
GoogleMapForASPNet1.GoogleMapObject.Points[“1”].Latitude += 0.003;
GoogleMapForASPNet1.GoogleMapObject.Points[“1”].Longitude += 0.003;
We are incrementing Latitude and Longitude value for Pushpin-1 here. Note that I am using ID(In above code “1”) of pushpin to set new Latitude and Longitude.
Run your application and click on Button. You will note that whole page get’s refreshed (or postback). To stop it from post back, you need to wrap this button with an Ajax Update panel. Go to Visual Studio toolbox and drag Ajax Updatepanel control on your page.
Move button inside update panel that we just placed.
Ty running website again and click on button. You should notice that now page is not posting back and Pushpin moves on map.
Auto refreshing map and GPS Navigation
You can use Ajax Framewor’s timer control in similar way as button control (I have explained above). On Timer_Tick() event you can specify new latitude longitude for all pushpins. This way Map will move all pushpins automatically after specified time delay. You can hook up any GPS service with this control to create GPS Navigation system.
Creating Polylines with Google Map control
Create points for polyline,
//Define Points for polygon
GooglePoint GP1 = new GooglePoint();
GP1.ID = “GP1”;
GP1.Latitude = 43.66675;
GP1.Longitude = -79.4042;
GooglePoint GP2 = new GooglePoint();
GP2.ID = “GP2”;
GP2.Latitude = 43.67072;
GP2.Longitude = -79.38677;
.//Define GP3,GP4,GP5,GP6 and GP7 in similar way
GooglePoint GP7 = new GooglePoint();
GP7.ID = “GP7”;
GP7.Latitude = 43.66656;
GP7.Longitude = -79.40445;
Create polyline between points GP1, GP2 and GP3
//Create Polygon using above points
GooglePolygon PG1 = new GooglePolygon();
PG1.ID = “PG1”;
//Give Hex code for line color
PG1.FillColor = “#0000FF”;
PG1.FillOpacity = 0.4;
//Stroke is outer border of polygon.
PG1.StrokeColor = “#0000FF”;
PG1.StrokeOpacity = 1;
PG1.StrokeWeight = 2;
//Add points to polygon
Add Polyline to Google Map control,
To enable or disable traffic overlay, set following property to true or false,
GoogleMapForASPNet1.GoogleMapObject.ShowTraffic = true;
Note: Traffic overlays may not be available in every region. To get more information on traffic overlays visit below link,
Go through samples provided in download. I have explained all sort of scenarios in which you may want to use google map control. If you have any questions, feel free to ask here.
I have published this article on www.codeproject.com as well. Here is the link to this article.
Sep 12, 2021
- Compiled source with Visual Studio 2019 Community Edition
- Checked into Github site for source control
Version 1.9.3 (Jan 24, 2017)
Following changes are done in this version.
- Fixed missing API Key issue. Google Map do not seem to allow rendering map without API key anymore. Enter API Key in web.config file. Use below section.
<add key=”GoogleAPIKey” value=”Your API Key Here“/>
Version 1.9.2 (Feb 11, 2014)
Following changes are done in this version
- Fixed issue with Directions, map type, icon image and height. Change of direction line color is possible now.
- Ability to change map type. Road map, satellite or hybrid is possible now. See example, MapWithSatelliteView.aspx.