Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 520

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 535

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 542

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 578

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/gathadam/public_html/wp-settings.php on line 18
Add Google Maps to your .Net site in 10 minutes
Previous     Next

Add Google Maps to your .Net site in 10 minutes

Ever wanted to add a Google Map to your site but only had 15 minutes to spare? Now you can add a map and still have time to brag to your mates and bask in the worship that (inevitably) comes afterward.

Basically, the guys over at subgurim.net have already done all the hard work in writing the .Net wrapper for Google Maps. Problem is, the examples on their site are mostly in spanish & its a bit difficult to find out exactly what is needed to get everything working.

But all this is cutting into your bragging time - so lets get started!

1. Get a Google Maps API key from here:
http://www.google.com/apis/maps/

2. Download the SubGurim wrapper dll from here:
http://en.googlemaps.subgurim.net/descargar.aspx

3. Unzip it, and put it in your \bin directory

4. Add it to your toolbox by
Tools -> Choose Toolbox Items -> Browse -> Select the .dll file -> OK
GMap will now be in the ‘Standard’ area of your Toolbox.

5. Add a new webpage.

6. Drag the GMap from the toolbox onto the page. A new instance of the GMap will appear on the page, with the name ‘GMap1′

7. Add the following lines to your web.config file:


  <appSettings>
    <add key="googlemaps.subgurim.net" value="YourGoogleMapsAPIKeyHere" />
  </appSettings>

8. Add the following code to your Page.Load sub


        Dim sStreetAddress As String
        Dim sMapKey As String = ConfigurationManager.AppSettings("googlemaps.subgurim.net")
        Dim GeoCode As Subgurim.Controles.GeoCode

        sStreetAddress = "100 Russell St. Melbourne. VIC. 3000. Australia"
        GeoCode = GMap1.geoCodeRequest(sStreetAddress, sMapKey)
        Dim gLatLng As New Subgurim.Controles.GLatLng(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng)

        GMap1.setCenter(gLatLng, 16, Subgurim.Controles.GMapType.GTypes.Normal)
        Dim oMarker As New Subgurim.Controles.GMarker(gLatLng)
        GMap1.addGMarker(oMarker)

Press F5, and start basking in the glory!

Update: I’ve now posted the code to add draggable markers and events to your Google Maps


If you liked this, then subscribe to my RSS feed

37 comments

  1. Jared Aug 23

    This is great. i was going to write a user control for googlemaps, now i dont have to. w00t.

  2. Gath Aug 24

    Hi to all the visitors from DotNetKicks,

    Glad you liked the article & thanks for voting (kicking!) me :)

    Cheers,
    Gath

  3. dested Aug 25

    Any chance on the source of that dll?

  4. Gath Aug 25

    Hi Dested,

    You’d have to ask the nice folks over at SubGurim.net for access to the source.

    Cheers,
    Gath

  5. Tomos Aug 27

    It’s any kind of opensource/GPL/freebie component like SubGurim.net made ???

    any idea what they are doing inside that component ??

  6. Dested Sep 6

    Tomos, All its doing is dynamically building the needed javascript to dump the map. Its not to difficult to do on your own, but im pretty sure this encompasses every function gmaps has to offer, which is more code than most care to write.

  7. srinivas Oct 5

    Ya This works fine in my local host. But when i upload it to real time web server it show some error please check it once

    http://www.rakshanllc.com/srinugoogle/default.aspx

    why this error occure

    I register with google and get google api key.
    I used it in my application exactly how u specify in this article.

    please reply me

  8. Gath Oct 5

    Hi srinivas,

    Please turn the custom errors off so that we can see the error message. You can see it on your local machine, but we aren’t getting anything.

    Cheers,
    Gath

  9. Jim Oct 12

    I have an API key that works fine when I write my own javascript. But this control keeps saying I need a key. Any idea why that might be happening?

  10. Gath Oct 12

    Hi Jim.

    Did you add the API key to your web.config file?

    Cheers,
    Gath

  11. Naimish Oct 26

    Hi,
    Its gr8 to use this Gmaps.dll. But any 1 know How to get Route from Gamps with turn by turn route details.

  12. Neil Nov 3

    Hi

    I adeded the control to my asp.net site, updated the web config file as per step 7 but I get the following error:
    Any help would be appreciated.

    Compiler Error Message: BC30451: Name ‘GMap1′ is not declared.

    Source Error:

    Line 34:
    Line 35: sStreetAddress = “100 Russell St. Melbourne. VIC. 3000. Australia”
    Line 36: GeoCode = GMap1.geoCodeRequest(sStreetAddress, sMapKey)
    Line 37: Dim gLatLng As New Subgurim.Controles.GLatLng(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng)
    Line 38:

  13. Gath Nov 3

    Hi Neil,

    This error is caused if you have not added the map control to your page, or you have added it to the page but called it something else (eg - GMap2).

    Cheers,
    Gath

  14. Neil Nov 3

    Hi Gath,

    Thanks for the reply. I checked the name of the map control & it
    is Gmap1. I have the following code in my default.apsx page:

    default.aspx:
    =========

    default.aspx.vb:
    ==========
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim sStreetAddress As String

    Dim sMapKey As String = ConfigurationManager.AppSettings(”googlemaps.subgurim.net”)

    Dim GeoCode As Subgurim.Controles.GeoCode

    sStreetAddress = “100 Russell St. Melbourne. VIC. 3000. Australia”

    GeoCode = GMap1.geoCodeRequest(sStreetAddress, sMapKey)

    Dim gLatLng As New Subgurim.Controles.GLatLng(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng)

    GMap1.setCenter(gLatLng, 16, Subgurim.Controles.GMapType.GTypes.Normal)

    Dim oMarker As New Subgurim.Controles.GMarker(gLatLng)

    GMap1.addGMarker(oMarker)

    End Sub

    ===============

    This code complains that Gmap1 is not declared.

  15. Neil Nov 3

    Hi Gath,

    Sorry I missed the default.aspx content..

    default.aspx:
    =========

  16. Neil Nov 3

    I am trying once more..

    default.aspx content:

    —-

    —-
    —-
    —-
    [I added -- at the end so I could upload these lines to the website here..]

  17. Neil Nov 4

    Hi Gath,

    I think I understand the problem but don’t know how to fix it.
    My application involves with uploading pictures. There is a
    page with detailsview control where file upload control, title,description etc. as inputs..Along with this, I would like user to point to a location in the map for “picture taken location” and my application should pickup
    latitude & longitude and insert in to the database columns in my picture table. I am not really sure how to do this yet but I can not at least enter code into pageload event as it complians about missing Gmap1 declartion.
    In details view, I added a template filed, and dragged the Gmap control to the template. Once I run the page I could see the map but as soon as I entered the following code under page load it complains Gmap1 is not declared.

    ================
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim sStreetAddress As String
    Dim sMapKey As String = ConfigurationManager.AppSettings(”googlemaps.subgurim.net”)
    Dim GeoCode As Subgurim.Controles.GeoCode
    sStreetAddress = “100 Russell St. Melbourne. VIC. 3000. Australia”
    GeoCode = GMap1.geoCodeRequest (sStreetAddress, sMapKey)
    Dim gLatLng As New Subgurim.Controles.GLatLng(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng)
    GMap1.setCenter(gLatLng, 16, Subgurim.Controles.GMapType.GTypes.Normal)
    Dim oMarker As New Subgurim.Controles.GMarker(gLatLng)
    GMap1.addGMarker(oMarker)
    End Sub
    ==============

    I tested the Gamp control outside detailsview control then it works fine no complains about Gmap1 declaration problem.

    Any suggestion would be appreciated.

    Many Thanks,

    Niel

  18. Gath Nov 5

    Hi Niel,

    I think I might know what is causing the problem.

    The issue is, that the detailsview control renders a new line for every record in its datasource. This means that you will possibly have more than one GMap control on the page - so you cannot just reference GMap1 in the Page.Load event.

    To set the marker location for each map, you would probably set them in the ‘ItemCreated’ event of the detailsview control.

    Cheers,
    Gath

  19. Durga Nov 26

    Hi Gath,

    can u give me an example for handling events, like clicking of map to give that location’s address…

    thanks and regards,
    Durga

  20. Sid Dec 3

    Hey Gath,

    I converted ur Vb code to C#. I also added the appsettings in the web.config file. I am getting an error when I debug it.

    //Inside web.config

    //in the page_load event
    string sStreetAddress;

    string sMapKey = ConfigurationManager.AppSettings["googlemaps.subgurim.net"];
    Subgurim.Controles.GeoCode GeoCode;

    sStreetAddress = “2411 York St. Des Moines. IA 50316″;
    GeoCode = GMap1.geoCodeRequest(sStreetAddress, sMapKey);
    Subgurim.Controles.GLatLng gLatLng;

    gLatLng(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng);

    GMap1.setCenter(gLatLng, 16, Subgurim.Controles.GMapType.GTypes.Normal);
    Subgurim.Controles.GMarker oMarker;

    oMarker(gLatLng);

    GMap1.addGMarker(oMarker);

  21. Gath Dec 11

    Hi Sid - what is the error?

  22. Tim Feb 28

    Hi Gath,

    Thanks for the article. Great help!

    I was having some troubles using the GGeoXML to display a KML. I know the KML is ok, its been tested. But I keep getting this “:(” error.

    I’ve tried on localhost and a dev server. All the other methods seem to work ok.

    Any ideas?

    Thanks,

    Tim.

  23. Sean Mar 17

    Any Idea why the map would not render when the control is incorporated into a vb.net ajax enabled website? Is it becasue the JavaScript is never getting to the browser level? Any insight would be appricated.

    Not getting any errors during comple or run… but just get a little gray box where the map should be.

    Thanks

    Sean

  24. Carlos Mar 20

    I converted it to C# and get the following error, anyonw have this issue?

    CS0176: Static member ‘Subgurim.Controles.GMap.geoCodeRequest(string, string)’ cannot be accessed with an instance reference; qualify it with a type name instead

    //code below
    string streetaddress;
    string smapkey = ConfigurationManager.AppSettings["googlemaps.subgurim.net"];
    Subgurim.Controles.GeoCode geocode;

    streetaddress = address1lbl.Text.ToString() + ” ” + address2lbl.Text.ToString();
    geocode = GMap1.geoCodeRequest(streetaddress, smapkey);

    Subgurim.Controles.GLatLng gLatLng = new Subgurim.Controles.GLatLng(geocode.Placemark.coordinates.lat, geocode.Placemark.coordinates.lng);

    GMap1.setCenter(gLatLng, 16, Subgurim.Controles.GMapType.GTypes.Normal);
    Subgurim.Controles.GMarker oMarker = new Subgurim.Controles.GMarker(gLatLng);
    GMap1.addGMarker(oMarker);

  25. Koen Apr 20

    Hey,

    I’ve got a google map in a modalpopupextender.
    And the map is only half showing.

    Does anyone have a workaround or solution?

    thnx

  26. sachu May 18

    When using asp .net code for the same above application, what namespace can be used.

    I have tried the same steps, you specified. But i am not able to find any namespace to put before Page load.

  27. Serge May 28

    Hi,
    Very helpfull, but what i am trying to do is to set coordinates instead of using an address, do you know how to do this?

    Thanks

  28. Amarjit Singh May 28

    HI Gath,

    First of all thanks a lot for the article it saved my life.

    Please tell me how can i add the option for all the three types of maps (hybrid, sattelite , Normal) on the this google map .

    Also please tell me how can add the zoomer on the google map .

    I need my google map look similar to http://www.arenaletting.co.uk/tenants-search.php

    please help me ..
    Thanks
    Amarjit

  29. Vik Jun 9

    Hi,
    Thanks for posting the code. It worked for me. do you have any code sample for dirctions also.

    Thank you!
    VK

  30. Xeshan Jun 10

    Hey man …thanks for a great article..but can u help me adding ploygons on the maps…..i needed to highlight an area…

  31. modeE Jun 23

    I haven’t seen a such great map control like this before.
    My question is how can I get the clicked point on the map?

    I need people to drag the map until they found their place then once they click on it, app could get the coordinates which I can save it as a favourite place.

    Any idea?
    I used this:
    sStreetAddress = “Al Ain, Jimi St., 127″;

    GeoCode = GMap1.geoCodeRequest(sStreetAddress);

    Subgurim.Controles.GLatLng gLatLng =

    new Subgurim.Controles.GLatLng(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng);

    GMap1.setCenter(gLatLng, 16, Subgurim.Controles.GMapType.GTypes.Normal);

    Subgurim.Controles.GMarker oMarker = new Subgurim.Controles.GMarker(gLatLng);

    GMap1.addInfoWindow(new Subgurim.Controles.GInfoWindow(oMarker, “??????? ???? ?????????”));

    GMap1.addGMarker(oMarker);

    then I could be able just to get the oMarker coordination but I failed to get any dynamic location could be selected by any other users.
    Problem is in this line:
    Subgurim.Controles.GLatLng gLatLng =

    new Subgurim.Controles.GLatLng(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng);

    GeoCode is defined with a specific Code Request “Street/address, .. etc” which is the problem.

  32. Senzora Jul 15

    Hey Gath

    Im a 3rd year student @ Durban University of Technology in South Africa. Now inorder for me to graduate I need to develop a real Windows/Web application. Which I am already developing a website, which also needs map. Now I need your assistance with Mapping..PLEASE

  1. Geek Daily » Blog Archive » Add Google Maps to your .Net site in 10 minutes
  2. Add Draggable Markers and Events to your Google Maps
  3. Draggable Marker Google Maps Demo
  4. Как добавить Google карту на .NET-сайт за 10 минут… « Блог Серёжи Борзова
  5. Olkenava » Blog Archive » Add Google Maps to your .Net site in 10 minutes