javascript - 位置 Java 脚本在部署后停止工作

标签 javascript asp.net-mvc google-location-services

当我在登录屏幕中本地调试我的网站时,我会看到我所在的 map ,并且有两个文本字段填充了我的经度和纬度,但是当我部署我的网站时,这将停止工作。奇怪的是,当我去年部署它时,它运行得很好。

这是登录页面的 View :

@using WebApplication2.Models
@model LoginViewModel

@{
    ViewBag.Title = "Log in";
}

<h2>@ViewBag.Title.</h2>
<div class="row">
    <div class="col-md-5">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" })) {
                @Html.AntiForgeryToken()
                <h4>Use a local account to log in.</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <div class="checkbox">
                            @Html.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe)
                        </div>
                    </div>
                </div>
                <input type="hidden" id="coordinates" name="coordinates">

                <div class="form-group">
                    <div class="col-md-10">
                        @Html.HiddenFor(m => m.Latitude)
                        @Html.EditorFor(m => m.Longitude)
                        @Html.EditorFor(m => m.Accuracy)
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Log in" class="btn btn-default" />
                    </div>
                </div>

                <p>
                    @Html.ActionLink("Register as a new user", "Register")
                </p>
                @* Enable this once you have account confirmation enabled for password reset functionality
                    <p>
                        @Html.ActionLink("Forgot your password?", "ForgotPassword")
                    </p>*@



            }
        </section>
    </div>

    @*<div class="col-md-4">
            <section id="socialLoginForm">
                @Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { Action = "ExternalLogin", ReturnUrl = ViewBag.ReturnUrl })
            </section>
        </div>*@
    <div class="col-md-4">
        <section>
            <h2>Contact administrator if its not your current location: </h2>
            <div id="map" style="height: 253px ; width: 253px" />
        </section>
    </div>

</div>

@section Scripts {
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script>
        var options = {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 0
        };
        alert('aaa');
        var x = document.getElementById("coordinates");
        var latitude = document.getElementById("Latitude");
        var longitude = document.getElementById("Longitude");
        var accuracy = document.getElementById("Accuracy");
        function getLocation() {
            if (navigator.geolocation) {
                var position = navigator.geolocation.getCurrentPosition(showPosition, null, options);

            } else {
                x.innerHTML = "Geolocation is not supported by this browser.";
            }
        }
        function showPosition(position) {
            x.value = "Latitude: " + position.coords.latitude + "Longitude: " + position.coords.longitude + "Accuracy: " + position.coords.accuracy + " meters";
            latitude.value = position.coords.latitude;
            longitude.value = position.coords.longitude;
            accuracy.value = position.coords.accuracy;
            InitializeMap(position)
        }
        var map;
        var geocoder;
        function InitializeMap(position) {
            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var mapOptions =
            {
                zoom: 16,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                disableDefaultUI: true
            };
            map = new google.maps.Map(document.getElementById("map"), mapOptions);

            var marker = new google.maps.Marker({
                position: latlng,
                title: 'Hello World!'
            });
            marker.setMap(map);

        }
        window.addEventListener('load', getLocation);
    </script>
    @Scripts.Render("~/bundles/jqueryval")
}

这就是它在本地的样子:

enter image description here

您可以在网络上看到它的样子:http://informatyka4445-001-site1.itempurl.com/Account/Login?ReturnUrl=%2F

问题:为什么当我将 map 部署到网络上时, map 不可见。

登录名是:admin@admin.pl 密码是:TestPass44!它完全是模拟数据,您可以使用它。

最佳答案

这是启动您的网站后我在控制台中看到的内容:

getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See link for more details.

将您的网站切换为 https 会有帮助

关于javascript - 位置 Java 脚本在部署后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39539215/

相关文章:

javascript - 将放置目标附加到拖动的对象

Javascript:如何修改我的代码以在主题发布后添加回消失的占位符

c# - 更新用户存储身份框架 3.0

android - 在检查位置设置的情况下,即使用户按下 'OK"按钮,也始终获取 RESOLUTION_REQUIRED

javascript - 使用草稿js的Nextjs-当我使用convertFromHTML方法使用HTML内容初始化编辑器时未定义文档

asp.net - 如何在 ASP.NET MVC 中获取相对路径

asp.net-mvc - MVC : System. Web.HttpException:在 Controller 上找不到公共(public)操作方法 'Delete'

ios - 获取用户的当前位置google api不起作用

java - 在使用 Job Scheduler API 的作业服务中使用 GoogleApiClient - 缺少回调

javascript - 如何使用 jQuery 添加选项到 HTML 选择