Google
Web Netomatix

How to insert google map in each row of DataGrid

by Admin 2. July 2009 05:19

Download Sample Project

google map in grid

While working on a car dealership listings web site, I was experimenting with inserting google maps into the data grid. The grid will show name and address of top rated car dealers in certain categories. And along side their information, it will show their location on google map. To accomplish this task, I needed the longitude and latitude of car dealership's address or location. Some time back in my earlier post Convert Address and/or IP address to Geo Location I described how you can utilize google map's api to convert a physical address to geo location coordinates. This is the same technique that I used in Backyardtweets to show tweets in a specified location. So use the google map api to get these coordinates and insert these coordinates in the java script.

Now that you have geo location corresponding to an address, your task is to insert that little piece of java script in each row of the data grid. The way google map java script works is that it requires a HTML element where it can insert the map image. So I inserted a div with unique id in a cell in each row and then passed that unique id to java script that is used to render the map. Add an event handler for RowDataBound event and there you can add the element and register a client script for map as well. Here is some code snippet that shows how this all is accomplished.


protected void OnDealerRowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
	var carDealer = e.Row.DataItem as CarDealer;
	var divId = string.Format("dealloc_{0}", carDealer.Id);
	var mapPanel = e.Row.Cells[2].FindControl("mapPanel") as Panel;
	var div = new HtmlGenericControl("div");
	div.Attributes.Add("id", divId);
	div.Attributes.Add("class", "mapdiv");
	mapPanel.Controls.Add(div);
	string js = GetGoogleMapScript(carDealer, divId);
	ScriptManager.RegisterStartupScript
	  (this.Page, this.GetType(), "_map_" + carDealer.Id, js, true);
 }
}

private string GetGoogleMapScript(CarDealer dealer, string ctlId)
{
 string loc = string.Empty;
 if (!string.IsNullOrEmpty(dealer.State))
 {
	loc = string.Format("{0},", dealer.State);
 }
 loc += dealer.Country;
 decimal longitude, latitude;
 GoogleMapUtility.GetGeoLocationFromGoogle(loc, out longitude, out latitude);
 dealer.Location.Latitude = latitude;
 dealer.Location.Longitude = longitude;
 return GetJScriptForGeoLocations(longitude, latitude, ctlId);
}

public static string GetJScriptForGeoLocations(decimal longitude, decimal latitude, string elemId)
{
 StringBuilder sb = new StringBuilder();
 sb.Append("function initialize_" + elemId + "() {{");
 sb.Append(" if (GBrowserIsCompatible()) {{ ");
 sb.Append(" var map = new GMap2(document.getElementById(\"{0}\"));");
 sb.Append(" map.setCenter(new GLatLng({1}, {2}), 13);");
 sb.Append(" map.setUIToDefault();}}}} initialize_" + elemId + "();");
 return string.Format(sb.ToString(), elemId, latitude, longitude);
}

The attached project with this post has all the implementation for this sample implementation. And this is a Visual Studio 2010 project. I have not used any 2010 specific namespaces or code in this implementation so you should be able to take all the code and move it into a VS2008 or VS2005 project. And you will need to get key for google map api use from google as well.

Feel free to drop us a line if you have something in mind that you would like us to prototype and write about it.

Views: 3244

Tags: , ,

ASP.Net | GridView | DataGrid

Comments

7/2/2009 6:45:25 AM #

trackback

How to insert google map in each row of DataGrid

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

11/18/2009 2:22:59 AM #

replica watches

Hi,I find you website through google,I was deeply interested by your post,Thanks for your great work.

replica watches

11/25/2009 6:27:00 AM #

cash loans

Interesting post

cash loans United States

11/28/2009 5:20:06 AM #

jeux de blackjack en ligne gratuits

.NET now works with most commercial and open source databases such as MySQL, SQL Server, VistaDB and many others. This allow you to use practically any database supported by your hosting provider. You can still use XML files if you don't want to use a database.

jeux de blackjack en ligne gratuits India

12/5/2009 6:09:33 AM #

fast payday loans

Nice resource. rss feed added

fast payday loans United States

12/6/2009 5:44:23 PM #

payday loans

Just try to smile for about 2-3 mins then you can get back to work

payday loans United States

12/9/2009 3:14:08 AM #

Cheap new cars

We can now find you the best deals for the new car you are looking for by searching across our panel of manufacturer dealers across the whole of the UK.

Cheap new cars United States

12/21/2009 7:43:33 AM #

Louisiana Payday Loans

Interesting post

Louisiana Payday Loans United States

12/30/2009 9:57:05 AM #

loans

I always wanted to write in my site something like that but I guess you'r faster :)

loans United States

1/2/2010 4:05:35 AM #

online payday loans

Interesting post

online payday loans United States

1/6/2010 11:54:31 PM #

wii Bundles In Stock

Happy to see your blog as it is just what I’ve looking for and excited to read all the posts.I am looking forward to another great articles from you

wii Bundles In Stock United States

1/7/2010 1:01:03 AM #

HP Quick Test Professional

I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commentators here!

HP Quick Test Professional United States

1/10/2010 11:16:14 PM #

SEO Services

This article simply ROCKS ! That was a great read for me. keep it up with all the good work..

http://www.clickresponse.net/

SEO Services United States

1/12/2010 3:21:55 AM #

Web designing company india

That is some inspirational stuff. Thanks a lot for sharing with us

Web designing company india United States

1/18/2010 1:48:23 PM #

payday loans

You cannot plough a field by turning it over in your mind.

payday loans United States

1/19/2010 6:27:36 AM #

Website SEO

Good post….thanks for sharing.. very useful for me i will bookmark this for my future needed. thanks for a great source.

Website SEO United States

1/22/2010 12:33:54 AM #

American online gambling site

If anything about current interaction design can be to called “glamorous,” it’s creating Web applications. After all the, when was last time you heard someone rave about the interaction design of a product that wasn’t on the Web? (Okay, besides the iPod.) All the cool, innovative new projects are online

American online gambling site United States

1/25/2010 11:33:07 PM #

car dealers Dubai

This article addressed that very important information that they make to the knowledge society. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.

car dealers Dubai U.A.E.

1/25/2010 11:34:11 PM #

car dealers Dubai

This article addressed that very important information that they make to the knowledge society. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.

car dealers Dubai U.A.E.

2/4/2010 5:15:26 AM #

ucvhost

I found lots of interesting information here. The post was professionally written and I feel like the author has extensive knowledge in the subject. Keep it that way.

ucvhost United States

2/6/2010 7:24:57 AM #

ucvhost

How to use Remote Desktop Connection for the two systems which have same IP Address?

ucvhost United States

2/10/2010 3:32:58 AM #

Sweepstakes

This Street View demo map by Lim Chee Aun shows how to rotate a Google Map so that it faces the same direction as a Street View image.

Sweepstakes United States

2/12/2010 3:12:32 AM #

ucvhost

Hi. I wanted to drop you a quick note to express my thanks. I've been following your blog for a month or so and have picked up a ton of good information as well as enjoyed the way you've structured your site.

ucvhost United States

2/13/2010 5:33:02 AM #

cadillac Car Radio

Sells replacement factory car radios.  Don't accept aftermarket radios, get a factory OEM car radio back in your vehicle.  Easy to use online car radio catalog.  Free Shipping.

cadillac Car Radio United States

2/13/2010 5:33:34 AM #

daewoo a/c compressor

Offers auto air conditioning parts including ac compressor, ac condenser and more. Toll free phone support and full warranty on all ac parts.  Free Shipping and 30 day return policy.

daewoo a/c compressor United States

2/17/2010 12:12:48 AM #

canon ink cartridges

Very well expressed the topic. Thanks for the code. Its very useful for my assignment may be I got the highest marks for this.

canon ink cartridges India

2/17/2010 3:56:05 AM #

Giocare al videopoker

I have the following problem w.r.t DataGrid. The first column header of my DataGrid is a checkBox and first column of every row contains CheckBox. I have done this using HeaderRenderer and ItemRenderer.

Giocare al videopoker United States

2/18/2010 10:10:13 AM #

Bob

Handy!

Bob United States

2/19/2010 10:46:26 PM #

bookmarksubmission

I’ve been looking around for articles similar to this but never found one that actually was valuable such as this. Glad I found this place

bookmarksubmission United States

2/22/2010 1:38:51 AM #

colorado springs mortgage

I am not much into reading, but somehow I got to read nice information on your site. Simple to understand and helpful. We will look forward for your future updates.

Thanks

colorado springs mortgage United States

2/27/2010 1:51:15 AM #

Auto Accessories

There are several places where you can buy these auto parts aftermarket.

Auto Accessories United States

3/1/2010 7:33:30 AM #

web designing Dubai

So thats how it is done. Thanks for sharing info regarding Google Map. Your post helped a lot.

web designing Dubai U.A.E.

3/2/2010 12:08:57 AM #

SEO Services India

There are very few people in this world who gives such tremendous views. I appreciate your work and hopping for some more informative posts.

Thanks

SEO Services India United States

3/5/2010 12:18:02 AM #

SEO Services Florida

SearchXCEL is the industry leading, Best SEO Service Providers and Search Engine Optimization (SEO) Company offering Affordable SEO services to improve website rankings in all the major search engines including Google, Yahoo and Bing for businesses of all sizes throughout the US and the world. Call 877.400.0252 to Request a Free SEO Analysis.

SEO Services Florida United States

3/6/2010 12:46:49 AM #

unsecured loans

i would like to recommend your blog post for my friends because it has a great potential and great content.

unsecured loans United States

3/26/2010 11:14:06 PM #

Registry Cleaner software

bookmared...Really enjoyable .thanks

Registry Cleaner software United States

3/29/2010 4:39:49 AM #

ONLINE SWEEPSTAKES

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

ONLINE SWEEPSTAKES United States

3/30/2010 5:27:29 AM #

tungsten rings

Very nice information post on your blog. I got very nice and useful tips. Thanks for sharing. We will look forward for your future updates.

tungsten rings United States

3/31/2010 3:54:12 AM #

SEO COMPANY

Thanks for the informative post i was been looking for this info and finally got here.

SEO COMPANY United States

3/31/2010 3:54:58 AM #

bingo in rete

I have the following problem w.r.t DataGrid. The first column header of my DataGrid is a checkBox and first column of every row contains CheckBox. I have done this using HeaderRenderer and ItemRenderer. The requirement is to check all the checkBox in the all the rows on clicking of the checkBox in the header. It is similar to the behavior of yahoomail or gmail, where on click of a checkbox in the header checks all the checkBox in all the rows. So, basically I need a way to traverse/loop through each row of a dataGrid.

bingo in rete Sweden

3/31/2010 12:46:25 PM #

SEOcompany

Interesting post. Also embed maps built with google mymaps into your blog

SEOcompany Canada

4/1/2010 1:38:06 AM #

Parking In Manhattan

Google map that residents could use to indicate how loud the sound was in their area.

Parking In Manhattan United States

4/9/2010 12:10:48 PM #

high life style

When it is dark enough, you can see the stars.

high life style United States

4/14/2010 3:31:07 AM #

Seo agency uk

Useful!

Seo agency uk United Kingdom

4/18/2010 2:59:25 PM #

payday loans

Good business leaders create a vision, articulate the vision, passionately own the vision, and relentlessly drive it to completion.

payday loans United States

4/23/2010 9:36:32 AM #

online payday advance

The secret of a leader lies in the tests he has faced over the whole course of his life and the habit of action he develops in meeting those tests.

online payday advance United States

5/12/2010 4:05:35 AM #

ink cartredges

Wow, I never knew that Inkjet Color. That’s pretty interesting...

ink cartredges United States

5/18/2010 10:13:04 PM #

movers and packers

You made some good points there. I did a search on the topic and found most people will agree with your blog.

movers and packers India

5/19/2010 1:43:55 AM #

Free Job Postings | video classifieds

Thanks for sharing this powerful information.That's very helpful and interesting.

Free Job Postings | video classifieds India

5/19/2010 3:00:39 AM #

migliori Bonus casinò del Web

Thank you! No one had covered how to work buttons into datagrids, and they were giving me fits! Thanks for the simple and obvious solution.

migliori Bonus casinò del Web India

5/25/2010 12:45:59 AM #

notebook battery supplier

ogle map java script works is that it requires a HTML element where it can insert the map image. So I inserted a div with unique id in a cell in each row and then passed that unique id to java script that is used to render the map. Add an event handler for RowDataBound event and there you can add the element and register a client script for map as well. H

notebook battery supplier Oman

5/25/2010 9:46:49 PM #

Packers and movers in pune

I was very pleased to find this site.I wanted to thank you for this great read!

Packers and movers in pune India

6/7/2010 3:05:42 PM #

bates boots




I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post




bates boots United States

6/9/2010 2:05:27 AM #

Top packers and movers in pune

I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day!

Top packers and movers in pune India

6/9/2010 6:11:25 AM #

Dubai Cars

Resources like the one you mentioned here is very useful! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Dubai Cars U.A.E.

6/17/2010 12:22:26 AM #

bulk natural peanut butter

I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you.

bulk natural peanut butter United States

6/18/2010 6:13:22 AM #

Agile Informatics

your task is to insert that little piece of java script in each row of the data grid. The way google map java script works is that it requires a HTML element where it can insert the map image. So I inserted a div with unique id in a cell in each row and then passed that unique id to java script that is used to render the map.

Agile Informatics United States

6/23/2010 2:45:05 AM #

packers and movers india

Nice your Posting...
    Thank You........

packers and movers india India

6/23/2010 2:53:05 AM #

movers and packers in pune

Great thread.  Enjoyed the posts..

movers and packers in pune India

6/28/2010 5:18:06 AM #

Dubai Rentals

I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.

Dubai Rentals United States

7/1/2010 2:30:13 AM #

chase auto loan

Good for me find out the addresses. Thanks

chase auto loan Algeria

7/1/2010 2:32:06 AM #

chase personal loans

It is interesting to know this page. From this site I can know how to insert google map in each row of DataGrid easier.

chase personal loans Albania

7/2/2010 2:04:01 AM #

Automobile Moving  in pune

nice posting

Automobile Moving in pune India

7/2/2010 11:16:58 PM #

Tour Bus Facilitator in pune


I have been reading your post regularly. They are highly informative and helpful.

Tour Bus Facilitator in pune India

7/6/2010 1:09:23 AM #

coach purses


I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing!

coach purses United States

7/6/2010 10:29:33 PM #

Tour Bus Facilitator in pune

I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.

Tour Bus Facilitator in pune India

7/7/2010 8:38:12 PM #

ab circle previews



This article indeed helps a lot in gathering knowledge on the importance of script.It is very helpful for call centers.Nice update.

ab circle previews Afghanistan

7/12/2010 7:18:14 PM #

Louis Vuitton Outlet

they are  specialized in getting long term higher rankings by boosting your SEPRs. i have gone through that i got lots of  experience from them..

Louis Vuitton Outlet United States

7/16/2010 6:46:14 AM #

Freelance Flash Designer

The DataGrid is one of the most powerful Windows® Forms controls. By using default DataGrid property settings you can easily bind a DataSet to a DataGrid to create an impressive Windows Forms user interface—one that allows users to drill down through multiple levels of hierarchical data and sort and reorder columns.

Freelance Flash Designer United States

7/23/2010 5:39:51 AM #

casino en ligne

First when user click on Showmap call the function closemap and than call function for showmap.and add exception in Jquery/JS for better performance.first check how much row in datalist and what is the Control Id for each row and than use closemap function to close the map.

casino en ligne United States

7/28/2010 1:52:56 AM #

apartments in dubai

I think this post will be a fine read for my blog readers too, could you please allow me to post a link to my blog. I am sure my guests will find that very useful.

apartments in dubai United States

7/28/2010 2:15:24 AM #

Real Estate Agent Management

I wanted to thank you for this great blog! I really enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

Real Estate Agent Management United States

Add Comment


(Will show your Gravatar icon)

  Country flag


  • Comment
  • Live Preview
Loading