简体   繁体   English

Google会在没有位置的情况下将地图设置为默认位置

[英]Google maps if no location set marker to default location

I have google maps which work fine when location can be detected, but if it can't, map is never shown. 我有谷歌地图,可以在检测到位置时很好地工作,但如果不能,则永远不会显示地图。 What I want to do is if location can't be detected set lat and lng variable to the one in function locError. 我想做的是,如果无法检测到位置,请将lat和lng变量设置为locError函数中的一个。 How can that be done? 那怎么办?

here's my code: 这是我的代码:

function initializeMap() {
    map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 19,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('test'));
}
var pinColor = "6495ED";
var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + pinColor,
    new google.maps.Size(21, 34),
    new google.maps.Point(0, 0),
    new google.maps.Point(10, 34));
var pinShadow = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_shadow",
    new google.maps.Size(40, 37),
    new google.maps.Point(0, 0),
    new google.maps.Point(12, 35));

function locError(error) {
    // the current position could not be located
    lat = 53.5409298;
    lng = -2.11136590000001;
    initializeMap();
    setCurrentPosition(position);
}

function setCurrentPosition(position) {
    if (position.coords.latitude) {
        lat = position.coords.latitude;
        lng = position.coords.longitude;
    } else {
        lat = 53.5409298;
        lng = -2.11136590000001;
    }
    var accuracy = position.coords.accuracy;
    currentPositionMarker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(
            lat,
            lng
        ),
        title: "Current Position",
        icon: pinImage,
        shadow: pinShadow,
        animation: google.maps.Animation.DROP
    });
    map.panTo(new google.maps.LatLng(
        lat,
        lng
    ));
    circle = new google.maps.Circle({
        map: map,
        radius: accuracy,
              strokeColor: '#E62C56',
              strokeOpacity: 0.8,
              strokeWeight: 2,
              fillColor: '#6495ED',
              fillOpacity: 0.10
    });

    circle.bindTo('center', currentPositionMarker, 'position')
    algolia_search(position);
}

function displayAndWatch(position) {
    // set current position
    setCurrentPosition(position);
    // watch position
    watchCurrentPosition(position);
}

function watchCurrentPosition(position) {
    var positionTimer = navigator.geolocation.watchPosition(
        function (position) {
            setMarkerPosition(
                currentPositionMarker,
                position,
            )
        });
}

function setMarkerPosition(marker, position) {
    circle.setRadius(position.coords.accuracy);
    marker.setPosition(
        new google.maps.LatLng(
            lat,
            lng)
    );
    algolia_search(position);
}

function initLocationProcedure() {
    initializeMap();
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
    } else {
        displayAndWatch();
        locError();
    }
}

function clearOverlays() {
    for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(null);
    }
    markers.length = 0;
}
$(document).ready(function () {
    initLocationProcedure();
});

What is causing an issue of the map not being loaded at all when location is off? 是什么原因导致位置关闭时根本无法加载地图的问题? How can I still load the map and bind it to lat and lng set in error function which is being triggered when location is not found 如何仍然加载地图并将其绑定到错误功能中设置的经纬度

I think you need to try like that if there there is no location in your loop or etc. 我认为如果您的循环等中没有位置,则需要尝试这种方法。

function initializeMap() {
          map = new google.maps.Map(document.getElementById('map'), {
            zoom: 19,
            center: new google.maps.LatLng(53.5409298,-2.11136590000001),
            mapTypeId: google.maps.MapTypeId.ROADMAP
          });

          var lat = 53.5409298;
            var lng = -2.11136590000001;
            var title = "Test" +" "+"Set";
            var latLng = new google.maps.LatLng(lat,lng);
            var marker = new google.maps.Marker({
                zoom: 12,
                position: latLng,
                map: map,
                title: title
              });

        }

I observed one thing more is that you did not give the center position in your map, so may be it will produce error. 我观察到的另一件事是您没有在地图中给出中心位置,因此可能会产生错误。

center: new google.maps.LatLng(53.5409298,-2.11136590000001),

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM