javascript - 谷歌地图蓝点在 reactjs 中的当前位置

标签 javascript reactjs google-maps

我正在实现谷歌地图..我想要当前位置的默认谷歌蓝点。任何人都可以告诉,如何在 reactjs 或简单的 javascript 中这样做..

最佳答案

首先您必须允许浏览器将您的地理位置发送到 Google API,然后在窗口/文档加载事件上连接此代码:

var map, infoWindow;
function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
        center: { lat: -34.397, lng: 150.644 },
        zoom: 6
    });
    infoWindow = new google.maps.InfoWindow;

    // Try HTML5 geolocation.
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            var pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };

            infoWindow.setPosition(pos);
            infoWindow.setContent('Location found.');
            infoWindow.open(map);

            var marker = new google.maps.Marker({
                position: pos,
                map: map,
                title: 'Hello World!',
                icon: 'https://maps.google.com/mapfiles/kml/shapes/info-i_maps.png',
            });

            map.setCenter(pos);
        }, function () {
            //handleLocationError(true, infoWindow, map.getCenter());
        });
    } else {
        // Browser doesn't support Geolocation
        //handleLocationError(false, infoWindow, map.getCenter());
    }
}

关于javascript - 谷歌地图蓝点在 reactjs 中的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51463936/

相关文章:

javascript - 错误类型错误 : Cannot read property 'toLowerCase' of undefined. Ionic 3

javascript - react : Dispatch not firing on route change

javascript - 如何在 react 中超出最大更新深度

ios - GMSAddress placeID属性丢失。

java - Google map Activity 显示空白屏幕

javascript - 如何使用 Angular js 从另一个数组创建 JSON 对象数组

javascript - 我试图删除一个逗号,但似乎无法删除,为什么?

javascript - 在同一页面上使用 Browserify 和 RequireJS?

javascript - 通过在没有前端渲染部分的情况下登录控制台来 react redux 测试

ios - iPhone 付费应用中的 Google Maps API