Is there a way to Fix a Google Maps Marker to the Center of its Map always?

前端 未结 7 1363
野趣味
野趣味 2020-12-01 00:21

To be more precise, what I try to accomplish is that while I Drag a Google Map there\'s a Google Maps Marker that stays fixed

7条回答
  •  旧巷少年郎
    2020-12-01 00:37

    here i have added geo location button map with fixed pin to accurate your location

    try it :)

     
    function loadScript(src,callback)
    { 
        var script = document.createElement("script");
        script.type = "text/javascript";
        if(callback)script.onload=callback;
        document.getElementsByTagName("head")[0].appendChild(script);
        script.src = src;
    }
      
      loadScript('https://maps.googleapis.com/maps/api/js?v=3&sensor=false&callback=initialize');
    
    function initialize() 
    		{   
        map = new google.maps.Map(document.getElementById('map_canvas'), {
              zoom: 13, 
            });
    latitudeTxt=document.getElementById('latitude');
    longitudeTxt=document.getElementById('longitude');
            marker = new google.maps.Marker({
              map: map,
              draggable: false,
              animation: google.maps.Animation.DROP,
              position: {lat: 59.327, lng: 18.067}
            });
            var position = marker.getPosition();
            if(typeof position !== 'undefined')
            {
                map.setCenter(marker.getPosition());
            }
     			
          	map.addListener('center_changed', () => {
              
               marker.setPosition(map.getCenter())
                //get current location when we move the map 
                var NewMapCenter = map.getCenter();
               var latitude = NewMapCenter.lat();
    					var longitude = NewMapCenter.lng();
               latitudeTxt.innerHTML =latitude;
               longitudeTxt.innerHTML =longitude;
            })
            
            addYourLocationButton(map, marker);
    		}
    
     function geolocate() 
        {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                var geolocation = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                };
                map.setCenter(geolocation); 
                });
            }
        }
     
     function addYourLocationButton (map, marker) 
    {
        var controlDiv = document.createElement('div');
    
        var firstChild = document.createElement('button');
        firstChild.style.backgroundColor = '#fff';
        firstChild.style.border = 'none';
        firstChild.style.outline = 'none';
        firstChild.style.width = '28px';
        firstChild.style.height = '28px';
        firstChild.style.borderRadius = '2px';
        firstChild.style.boxShadow = '0 1px 4px rgba(0,0,0,0.3)';
        firstChild.style.cursor = 'pointer';
        firstChild.style.marginRight = '10px';
        firstChild.style.padding = '0';
        firstChild.title = 'Your Location';
        controlDiv.appendChild(firstChild);
    
        var secondChild = document.createElement('div');
        secondChild.style.margin = '5px';
        secondChild.style.width = '18px';
        secondChild.style.height = '18px';
        secondChild.style.backgroundImage = 'url(https://maps.gstatic.com/tactile/mylocation/mylocation-sprite-2x.png)';
        secondChild.style.backgroundSize = '180px 18px';
        secondChild.style.backgroundPosition = '0 0';
        secondChild.style.backgroundRepeat = 'no-repeat';
        firstChild.appendChild(secondChild);
    
        google.maps.event.addListener(map, 'center_changed', function () {
            secondChild.style['background-position'] = '0 0';
        });
    
        firstChild.addEventListener('click', function () {
            var imgX = 0,
                animationInterval = setInterval(function () {
                    imgX = -imgX - 18 ;
                    secondChild.style['background-position'] = imgX+'px 0';
                }, 500);
    
            if(navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                    var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    map.setCenter(latlng);
                    clearInterval(animationInterval);
                    secondChild.style['background-position'] = '-144px 0';
                });
            } else {
                clearInterval(animationInterval);
                secondChild.style['background-position'] = '0 0';
            }
        });
    
        controlDiv.index = 1;
        map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(controlDiv);
    }
    body,html,#map_canvas{height:100%;margin:0;}
    #map_canvas .centerMarker{
      position:absolute;
      /*url of the marker*/
      background:url(http://maps.gstatic.com/mapfiles/markers2/marker.png) no-repeat;
      /*center the marker*/
      top:50%;left:50%;
      z-index:1;
      /*fix offset when needed*/
      margin-left:-10px;
      margin-top:-34px;
      /*size of the image*/
      height:34px;
      width:20px;
      cursor:pointer;
    }
    
    
    
    latitude

    longitude

提交回复
热议问题