﻿var map;
var markers = [];
var Bounds = new google.maps.LatLngBounds();
var circle;
var geocoder = new google.maps.Geocoder();

function OnPageLoad() {
    var myLatlng = new google.maps.LatLng(57.7270, 16.1661);

    var myOptions = {
        zoom: 6,
        center: myLatlng,
        navigationControl: false,
        navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
        mapTypeControl: false,
        mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU },
        streetViewControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById('gmap'), myOptions);

    //var bounds = new google.maps.LatLngBounds();
    var circlePoints = Array();

    with (Math) {
        var d = 60 / 6378.8; // radians
        var nCenter = myLatlng
        var lat1 = (PI / 180) * nCenter.lat(); // radians
        var lng1 = (PI / 180) * nCenter.lng(); // radians

        for (var a = 0; a < 361; a++) {
            var tc = (PI / 180) * a;
            var y = asin(sin(lat1) * cos(d) + cos(lat1) * sin(d) * cos(tc));
            var dlng = atan2(sin(tc) * sin(d) * cos(lat1), cos(d) - sin(lat1) * sin(y));
            var x = ((lng1 - dlng + PI) % (2 * PI)) - PI; // MOD function
            var point = new google.maps.LatLng(parseFloat(y * (180 / PI)), parseFloat(x * (180 / PI)));
            circlePoints.push(point);
            Bounds.extend(point);
        }

        var cOpt = {
            paths: circlePoints,
            strokeColor: "#FF0000",
            strokeOpacity: 0,
            strokeWeight: 2,
            fillColor: "#FF0000",
            fillOpacity: 0.2,
            map: map,
            center: nCenter
        }

        circle = new google.maps.Polygon(cOpt);
    }
}

window.onload = OnPageLoad;
