Google地图地理定位在Safari中运行,但在其他浏览器中无法使用(Chrome,Firefox,Opera,IE)

时间:2014-08-04 00:46:11

标签: javascript google-maps

我正在建立一个网站的问题。我正在尝试设置一个谷歌地图,将阴影区域显示为企业的服务区域,我还想做的是设置地理位置以在用户位置放置一个引脚,以便他们可以看到它们是否在内部服务区。

以下代码完全按照我希望的方式在Safari中运行,但是,它只能在其他浏览器(例如Chrome,IE,Firefox)上获得通用位置(在市中心)。

我对网络开发很新,我只是在学习,所以我有点困惑为什么会这样。

谢谢Ash。

`var locations = [];
var map,
    marker,
    infowindow;

function initialize() {
    infowindow = new google.maps.InfoWindow();

    var myOptions = {
        zoom: 12,
        preserveViewport: true,
        center: new google.maps.LatLng(-26.720774, 153.084475),
        mapTypeId: google.maps.MapTypeId.ROADMAP

    };

    map = new google.maps.Map(document.getElementById("googleMap"), myOptions);

    var triangleCoords = [
        new google.maps.LatLng(-26.636872, 153.101323),
        new google.maps.LatLng(-26.631348, 153.070424),
        new google.maps.LatLng(-26.659732, 153.065618),
        new google.maps.LatLng(-26.673538, 152.998498),
        new google.maps.LatLng(-26.687343, 153.003819),
        new google.maps.LatLng(-26.709887, 153.021157),
        new google.maps.LatLng(-26.722920, 153.044847),
        new google.maps.LatLng(-26.773354, 153.043473),
        new google.maps.LatLng(-26.791130, 153.121408),
        new google.maps.LatLng(-26.791590, 153.148187),
        new google.maps.LatLng(-26.783775, 153.140634),
        new google.maps.LatLng(-26.733959, 153.135286),
        new google.maps.LatLng(-26.679981, 153.138917),
        new google.maps.LatLng(-26.677373, 153.118318)
    ];

    reflectshinesServiceArea = new google.maps.Polygon({
        paths: triangleCoords,
        strokeColor: '#6ab47b',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#6ab47b',
        fillOpacity: 0.35
    });

    reflectshinesServiceArea.setMap(map);

    set_current_location();
}

function add_location(description, latitude, longtitude) {
    locations.push([description, latitude, longtitude]);
}

function set_markers(fitbounds, map) {
    for (var i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });
        fitbounds.extend(marker.position);
        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }
}

function set_current_location() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            add_location('My location',
                position.coords.latitude,
                position.coords.longitude);

            set_markers(new google.maps.LatLngBounds(), map);
        }, function error(err) {
            console.log('error: ' + err.message);
            set_markers(new google.maps.LatLngBounds(), map);
        });
    } else {
        alert("Geolocation is not supported by this browser.");
    }
}




`

0 个答案:

没有答案