Fixed marker in center and drag map to get lat,long

前端 未结 1 1291
半阙折子戏
半阙折子戏 2020-12-09 07:35

I\'m an example map shows the current location with the click of a button shows lat,long ,and marker on the map is draggable to update lat,long, But I need a little change o

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 07:58

    If you want the centered marker from the jsfiddle you reference, you need to include this code from there (and its associated CSS):

    Code:

    $('
    ').addClass('centerMarker').appendTo(map.getDiv()) //do something onclick .click(function() { var that = $(this); if (!that.data('win')) { that.data('win', new google.maps.InfoWindow({ content: 'this is the center' })); that.data('win').bindTo('position', map, 'center'); } that.data('win').open(map); });

    CSS:

    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;
    }
    

    If you want the latitude and longitude to be populated with the coordinates of that marker (the center of the map), you need to add this code:

    google.maps.event.addListener(map,'center_changed', function() {
      document.getElementById('default_latitude').value = map.getCenter().lat();
      document.getElementById('default_longitude').value = map.getCenter().lng();
    });
    

    proof of concept fiddle

    complete code snippet:

    var map = null;
    var marker;
    
    function showlocation() {
      if ("geolocation" in navigator) {
        /* geolocation is available */
        // One-shot position request.
        navigator.geolocation.getCurrentPosition(callback, error);
      } else {
        /* geolocation IS NOT available */
        console.warn("geolocation IS NOT available");
      }
    }
    
    function error(err) {
      console.warn('ERROR(' + err.code + '): ' + err.message);
    };
    
    function callback(position) {
    
      var lat = position.coords.latitude;
      var lon = position.coords.longitude;
      document.getElementById('default_latitude').value = lat;
      document.getElementById('default_longitude').value = lon;
      var latLong = new google.maps.LatLng(lat, lon);
      map.setZoom(16);
      map.setCenter(latLong);
    }
    google.maps.event.addDomListener(window, 'load', initMap);
    
    
    
    function initMap() {
      var mapOptions = {
        center: new google.maps.LatLng(0, 0),
        zoom: 1,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);
      google.maps.event.addListener(map, 'center_changed', function() {
        document.getElementById('default_latitude').value = map.getCenter().lat();
        document.getElementById('default_longitude').value = map.getCenter().lng();
      });
      $('
    ').addClass('centerMarker').appendTo(map.getDiv()) //do something onclick .click(function() { var that = $(this); if (!that.data('win')) { that.data('win', new google.maps.InfoWindow({ content: 'this is the center' })); that.data('win').bindTo('position', map, 'center'); } that.data('win').open(map); }); }
    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;
    }
    
    
    
    
    

    0 讨论(0)
提交回复
热议问题