Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
How can I make a Google map and get the location from it to save it in the database webform c# 

What I have tried:

create a Google map and get the location from it to save it in the database webform
Updated 17-Jan-23 22:55pm

1 solution

Try following these steps to create a Google map in ASP.NET webform using C#:

1) Obtain a Google Maps API key from the Google Cloud Console.

2) In your ASP.NET webform, add a div element to the HTML where you want the map to be displayed. Give this div an ID, such as "map".

3) In the code-behind file (e.g. Default.aspx.cs), add the following script to the Page_Load event:

string mapKey = "YOUR_API_KEY";
string script = $"";
Page.ClientScript.RegisterStartupScript(this.GetType(), "GoogleMap", script);

4) Add a script block to the HTML that initializes the map and sets the location. For example:

    function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
            center: {lat: -34.397, lng: 150.644},
            zoom: 8

5) To get the location from the map and save it to the database, you can add a marker on the map and attach a click event listener to it. In the click event listener, you can get the marker's position and save it to the database using ADO.NET or Entity Framework.

marker = new google.maps.Marker({
                position: latLng,
                map: map,
                title: "Selected Location"

google.maps.event.addListener(marker, 'click', function (event) {
  var lat =;
  var lng = event.latLng.lng();
  //save lat and lng to database

I also recommend reading the Google Maps API documentation and other resources for more information. There are many details and considerations that need to be taken into account, such as error handling, styling, and performance.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900