【问题标题】:How to change Google Maps info window to a geolocation marker如何将 Google 地图信息窗口更改为地理位置标记
【发布时间】:2020-01-20 14:16:13
【问题描述】:

我遵循了 Google https://developers.google.com/maps/documentation/javascript/geolocation 的教程,因此我可以在地图上显示用户的位置。它目前看起来像这样,带有一个信息窗口:


但是我想显示一个地理位置标记而不是信息窗口:


这是我的代码:

function initialize() {

var mapOptions = {
    center: new google.maps.LatLng(52.520008, 13.404954)
    };
          infoWindow = new google.maps.InfoWindow;
          map = new google.maps.Map(document.getElementById("map"), mapOptions);

        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);
            map.setCenter(pos);
          }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
          });
        } else {
          // Browser doesn't support Geolocation
          handleLocationError(false, infoWindow, map.getCenter());
        }
      }

      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
                              'Error: The Geolocation service failed.' :
                              'Error: Your browser doesn\'t support geolocation.');
        infoWindow.open(map);
      } 

任何有关如何将其从信息窗口更改为地理位置标记的帮助都会很棒。提前致谢。

【问题讨论】:

标签: javascript google-maps


【解决方案1】:

要在谷歌地图中用标记显示位置,你可以试试这个:

var map = new google.maps.Map(document.getElementById("map"), {
    center: {lat: 52.520008, lng: 13.404954},
    zoom: 8,
    mapTypeId: 'roadmap'
});

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(52.520008, 13.404954),
    map: map,
    /*if you want to custom marker icon, you can set icon path here*/
    icon: 'your_icon_url',
    /* if you don't want to enable dragging, just remove this property */
    draggable: true
});

// if you enable dragging, you can initialize this event 
// to get latitude, longitude and address
google.maps.event.addListener(marker, 'dragend', function (marker) {
    var latLng = marker.latLng;
    var currentLatitude = latLng.lat();
    var currentLongitude = latLng.lng();

    var geocoder = new google.maps.Geocoder;
    var latlng = { lat: currentLatitude, lng: currentLongitude };
    geocoder.geocode({ 'location': latlng }, function (results, status) {
        if (status === 'OK') {
            // address
            console.log(results);
        } else {
            console.log('Geocoder failed due to: ' + status);
        }
    });
});

注意:要使用 Google 地图中的地点,您需要在 url 中要求 libraries=places 作为参数:

https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places

参考:更多谷歌设计的标记图标,可以参考这里:What are the filenames of new colored Google maps markers?

【讨论】:

    猜你喜欢
    • 2018-10-15
    • 2016-01-20
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 2011-02-24
    • 2014-09-26
    • 1970-01-01
    相关资源
    最近更新 更多