简体   繁体   English

Google Maps JavaScript API地理位置标记

[英]Google Maps JavaScript API Geolocation Marker

Im am trying to make it so that a marker appears when my Google Maps Javascript API determines user location. 我正在尝试这样做,以便当我的Google Maps Javascript API确定用户位置时出现标记。 When I open my HTML, it is able to pull up the user's location but there is no marker showing the user where they are located. 当我打开HTML时,它可以拉出用户的位置,但是没有标记显示用户所在的位置。 I have several markers that show the location of 3 gyms but I want to show the user where they are in relation to the three gyms. 我有几个标记可以显示3个体育馆的位置,但我想向用户展示它们相对于3个体育馆的位置。

Should I add the user's location in the "var markers =" array? 我应该在“ var markers =”数组中添加用户的位置吗? Or should I add some type of code after the "infoWindow.setPosition(pos);"? 还是应该在“ infoWindow.setPosition(pos);”之后添加某种类型的代码? Here is my code so far; 到目前为止,这是我的代码;

<script>
  var map, infoWindow;
  function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: {lat:34.0522,lng:-118.2437},
      zoom: 15,
      disableDefaultUI: true,
      gestureHandling: 'cooperative',
    });
    infoWindow = new google.maps.InfoWindow;

  if (navigator.geolocation) {

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

      infoWindow.setPosition(pos);

    }, function() {
      handleLocationError(true, infoWindow, map.getCenter());
    });
  } else {
     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);  }

    var markers = [
                  {
                    coords:{lat:34.051937,lng:-118.472118},
                    iconImage:'GymBoxNBurn.png',
                    content:'Box N Burn Boxing Gym'
                  },
                  {
                    coords:{lat:33.938454,lng:-118.277857},
                    iconImage:'GymBroadwayBoxing.png',
                    content:'Broadway Boxing Gym'
                  },
                  {
                    coords:{lat:34.015644,lng:-118.487396},
                    iconImage:'GymBoxNBurn.png',
                    content:'Box N Burn Boxing Gym'
                  }
                ];


    for(var i = 0;i < markers.length;i++){
        addMarker(markers[i]);
          }

            function addMarker(props){
              var marker = new google.maps.Marker({
                position:props.coords,
                map:map,
              });


              if(props.iconImage){
                marker.setIcon(props.iconImage);
                }

              if(props.content){
                var infoWindow = new google.maps.InfoWindow({
                  content:props.content
                  });

                  }
                }
              };

try this. 尝试这个。 Notice: it's not Google Maps that determines your position. 注意:决定您的位置的不是Google Maps。 I'ts standard HTML/javascript functionality; 我没有标准的HTML / javascript功能; your webbrowser reads it. 您的网络浏览器会读取它。

...
navigator.geolocation.getCurrentPosition(function(position) {
  var pos = {
    lat: position.coords.latitude,
    lng: position.coords.longitude
  };
  infoWindow.setPosition(pos);
  addMarker({
    coords: pos,
    content:'YOU ARE HERE'
  });  
  // if you want this, center the map.  Else remove next line
  map.setCenter(pos);          
}, 
...

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

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