地理位置更改标记位置

问题描述 投票:0回答:3

我使用地理定位,我可以查看地图我的坐标。然后,标记放置坐标。我想改变标记位置。我的代码在这里:

var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  lat=position.coords.latitude;
  lon=position.coords.longitude;
  latlon=new google.maps.LatLng(lat, lon)
  mapholder=document.getElementById('mapholder')
  mapholder.style.height='250px';
  mapholder.style.width='100%';

  var myOptions={
  center:latlon,zoom:14,
  mapTypeId:google.maps.MapTypeId.ROADMAP,
  mapTypeControl:false,
  navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
  };
  var map=new google.maps.Map(document.getElementById("mapholder"),myOptions);
  var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"});

这段代码得到了我的位置坐标。我该如何改变标记位置?

javascript html5 google-maps geolocation
3个回答
1
投票

我很抱歉Eren关于我之前的回答。我误解了。我想,这是正确的,你需要的。

显示标记移动的纬度和经度

Refer this site


1
投票

我在这个页面找到了一个解决方案。我的问题解决了。

refer thisthis site

var map;
var myCenter = new google.maps.LatLng(37, 35);
var markersArray = [];

function clearOverlays() {
    for (var i = 0; i < markersArray.length; i++) {
        markersArray[i].setMap(null);
    }
    markersArray.length = 0;
}

function initialize() {
    var mapProp = {
        center: myCenter,
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

    google.maps.event.addListener(map, 'click', function (event) {
        placeMarker(event.latLng);
    });
}

function placeMarker(location) {
    clearOverlays();
    var marker = new google.maps.Marker({
        position: location,
        map: map,
    });
    markersArray.push(marker);
    var infowindow = new google.maps.InfoWindow({
        content: 'Latitude: ' + location.lat() + '<br>Longitude: ' + location.lng()
    });
    infowindow.open(map, marker);
}

google.maps.event.addDomListener(window, 'load', initialize);

0
投票

moveBus()在initialize()之前被调用。尝试将该行放在initialize()函数的末尾。 Lat / Lon 0,0也不在地图上(它的坐标,而不是像素),所以当它移动时你看不到它。试试54,54。如果您希望地图的中心移动到新位置,请使用panTo()。

Demo: http: //jsfiddle.net/ThinkingStiff/Rsp22/

HTML:

<script src = "http://maps.googleapis.com/maps/api/js?sensor=false" > </script> <div id = "map-canvas"> </div>
CSS:

    #map-canvas {
        height: 400 px;
        width: 500 px;
    }

Script:

    function initialize() {

        var myLatLng = new google.maps.LatLng(50, 50),
            myOptions = {
                zoom: 4,
                center: myLatLng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            },
            map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),
            marker = new google.maps.Marker({
                position: myLatLng,
                map: map
            });

        marker.setMap(map);
        moveBus(map, marker);

    }

function moveBus(map, marker) {

    marker.setPosition(new google.maps.LatLng(0, 0));
    map.panTo(new google.maps.LatLng(0, 0));

};

initialize();
© www.soinside.com 2019 - 2024. All rights reserved.