简体   繁体   English

JS Google Maps API:如果地理位置关闭,请避免使用灰色地图

[英]JS Google Maps API: Avoid grey map if geo location is off

I want to display two markers on my view. 我想在我的视图中显示两个标记。 The first is static (the "goal") while the second is the current position of the user. 第一个是静态(“目标”),第二个是用户的当前位置。 If geo location is switched off or there are other reasons why the current location cannot be retrieved, only the first marker should be displayed. 如果关闭地理位置或者还有其他原因导致无法检索当前位置,则只应显示第一个标记。

This lead me to the following code: 这导致我得到以下代码:

        var aMarkerPos = [];

        // handle fixed position of goal
        var goalLatlng = new google.maps.LatLng(xx, yy);

        aMarkerPos.push(goalLatlng);

        this.geocoder = new google.maps.Geocoder();
        var mapOptions = {
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        this.map = new google.maps.Map(this.getView().byId("map_canvas").getDomRef(),
            mapOptions);

        var goalMarker = new google.maps.Marker({
            position: goalLatlng,
            map: this.map,
            title: 'GOAL'
        });

        var that = this;

        // Try HTML5 geolocation
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var currentLatlng = new google.maps.LatLng(position.coords.latitude,
                    position.coords.longitude);

                aMarkerPos.push(currentLatlng);

                var positionMarker = new google.maps.Marker({
                    position: currentLatlng,
                    map: that.map,
                    title: 'My Position'
                });

                // map: an instance of GMap3
                // latlng: an array of instances of GLatLng
                var latlngbounds = new google.maps.LatLngBounds();
                aMarkerPos.forEach(function(n){
                    latlngbounds.extend(n);
                });
                that.map.setCenter(latlngbounds.getCenter());
                that.map.fitBounds(latlngbounds);

            }, function(error) {
                that.map.setCenter(goalLatlng);
            });
        } else {
            this.map.setCenter(goalLatlng);
        }

The code works fine if geo location is active and the current position can be retrieved. 如果地理位置处于活动状态并且可以检索当前位置,则代码可以正常工作。 In all other cases, the view is just grey. 在所有其他情况下,视图只是灰色。 Why?? 为什么??

OMG, this was simple. 天哪,这很简单。 I just add it so that others might check my fault if they are doing the same: one has to set the center position (and the zoom factor) in the map settings. 我只是添加它,以便其他人可以检查我的错误,如果他们正在做同样的事情:一个人必须在地图设置中设置中心位置(和缩放系数)。

        var mapOptions = {
            center: goalLatlng,
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

As soon as I added them, the map did what I wanted... 我一添加它们,地图就做了我想要的......

These are the two required MapOptions : 这是两个必需的 MapOptions

center  LatLng  The initial Map center. Required.
zoom    number  The initial Map zoom level. Required.

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

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