Click here to Skip to main content
15,888,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
    
        #mapContainer {
            height: 500px;
            width: 800px;
            border: 10px solid #eaeaea;
        }
    
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDX1D37MapC2HfewVE0T3MXcUT4bstvHq8&callback=initMap" type="text/javascript"></script>
    <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">

        var geocoder;
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
        }
        //Get the latitude and the longitude;
        function successFunction(position) {
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
            codeLatLng(lat, lng)
        }

        function errorFunction() {
            alert("Geocoder failed");
        }

        function initialize() {
            geocoder = new google.maps.Geocoder();
        }

        function codeLatLng(lat, lng) {

            var latlng = new google.maps.LatLng(lat, lng);
            var mapOptions = {
                zoom: 15,
                center: latlng,
                mapTypeControl: true,
                navigationControlOptions:
                    {
                        style: google.maps.NavigationControlStyle.SMALL
                    },
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(
                         document.getElementById("mapContainer"), mapOptions
                         );
            geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    console.log(results)
                    if (results[1]) {
                        //formatted address
                        alert(results[0].formatted_address)
                        //find country name
                        for (var i = 0; i < results[0].address_components.length; i++) {
                            for (var b = 0; b < results[0].address_components[i].types.length; b++) {
                                //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                                if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                                    //this is the object you are looking for
                                    city = results[0].address_components[i];
                                    break;
                                }
                            }
                        }
                        // city data                  
                        alert(city.short_name + " " + city.long_name)
                    }
                    else {
                        alert("No results found");
                    }
                }
                else {
                    alert("Geocoder failed due to: " + status);
                }
                var marker = new google.maps.Marker({
                    position: latlng,
                    map: map,
                    title: "Your Location"
                });
            });
        }
    </script>
</head>
<body >
    
    <form id="form1"  runat="server">
    </form>
</body>
</html>


What I have tried:

2-3 hours, I'm looking on the internet and I try, but I did not get results. I'm trying another code block, latitude and longitude can save the database. This is not just what I wanted, and I want to save the city formatted address
(My English is not very good.I apologize if I am wrong :) )
Posted
Updated 30-Mar-16 7:26am
Comments
F-ES Sitecore 30-Mar-16 5:26am    
What you're doing is against the terms and conditions of google maps.
E.Açıkgöz 30-Mar-16 13:29pm    
Why is against?
F-ES Sitecore 31-Mar-16 4:32am    
Google forbid you from saving information you get from its map services.

10.5 d
E.Açıkgöz 31-Mar-16 7:38am    
University project assignments.I wanted it to be different.
Not one thing is clear to everyone :) :)

1 solution

you can call asp.net page method(WebMethod) from javascript. pass the details as parameters to web method and do the database save in the code behind.

follow below tutorial:
Call WebMethod from JavaScript with parameters in ASP.Net using C# and VB.Net[^]
 
Share this answer
 
Comments
E.Açıkgöz 30-Mar-16 13:37pm    
I've tried it, but it did not.

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