Search code examples
javascriptc#apigoogle-mapscode-conversion

Rewrite part of javascript with C#


I'm writing this program in C# which should display Google Maps. I'm using the Google Maps JavaScript API which is the best one I could find. With the program you should be able to search for places.

The code:

window.onload = function() {

    var latlng = new google.maps.LatLng(52.785804, 6.897585);
    var options = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map"), options);
}
html, body {
    margin: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

#map {
    width: 80%;
    height: 100%;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
    <title>Google Maps</title>
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>

</head>

<body>
    <div id="map">
    </div>
</body>

</html>

Am I in some way able to edit the latlng using C#? Or does someone know an alternative way to use the Google Maps API with C#?


Solution

  • If you are not using MVC or other server side technologies on this specific page your only option would be to load the lat/long from an AJAX call.

    $.ajax({
        url: "url/to/your/api/that/returns/lat/long",
        success: function(result) {
            // process your JSON result and set the lat/long
        }
    });
    

    the API can be written on the server side using any language (including c#)