-3

I am using asp.net with C#. I embedded Google Maps in my application using JavaScript. I input latitude and longitude, and its shows the location on Google Maps. Now I want that it takes lat & lng from a database and shows the location.

Here's my code:

  <script type="text/javascript"
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC6v5-2uaq_wusHDktM9ILcqIrlPtnZgEk&sensor=false">
    </script>
    <script type="text/javascript">
        function initialize() {
            var lat = document.getElementById('txtlat').value;
            var lon = document.getElementById('txtlon').value;
            var myLatlng = new google.maps.LatLng(lat, lon) 
            var mapOptions = {
                center: myLatlng,
                zoom: 18,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                marker: true
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
            var marker = new google.maps.Marker({
                position: myLatlng
            });
            marker.setMap(map);
        }


    </script>
    </head>

    <body >

    <table>
    <tr>
    <td class="style3">Enter Latitude:</td>
    <td><input type="text" id="txtlat" value="" onclick="return txtlat_onclick()" /> </td>
    </tr>
    <tr>
    <td class="style3">Enter Longitude:</td>
    <td><input type="text" id="txtlon" value="" onclick="return txtlon_onclick()" /> </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input type="button" value="Submit" onclick="javascript:initialize()" /> </td>
    </tr>
    </table>
    <div id="map_canvas" style="width: 535px; height: 347px"></div>


    </body>
    </html>
Pondlife
  • 15,992
  • 6
  • 37
  • 51

1 Answers1

0

Do an ajax request to the server, the server will connect to the database, read the lat and longitude from a table and return. On your ajax handler, replace your lat and lon variables with the response.

Checkout this question: jQuery ajax call to REST service for an idea of how the javascript should look.

Checkout this article on how to build a restful api in c# http://code.msdn.microsoft.com/Build-truly-RESTful-API-194a6253.

Checkout this article on how to connect to a database with c# http://www.daniweb.com/software-development/csharp/threads/21636/how-do-you-connect-to-a-sql-database-using-c

Community
  • 1
  • 1
Jose Vega
  • 10,128
  • 7
  • 40
  • 57
  • 2
    @user2347571 You've been given some good suggestions and useful links, and there are already [many questions](http://stackoverflow.com/search?q=javascript+query+sql+server) on this site about using JavaScript with SQL Server. I suggest that you try implementing something yourself and if you have specific problems then feel free to post them as questions. – Pondlife May 03 '13 at 20:54