【问题标题】:getting moving gelocation on bing maps在 bing 地图上获取移动地理位置
【发布时间】:2015-08-28 07:53:24
【问题描述】:

需要获取动态位置图钉而不是移动到我从 html5 地理定位中获得的位置。我尝试了其他一些事情,但图钉不断生成重复项。删除图钉也有用。

var map = null;
var p = 0;
var x = document.getElementById("notice");
$(document).ready(function(){
  getLocation();
  watchPosition();
});
function watchPosition() {
  //map options
  var mapOptions = {
    credentials: "key here",
    mapTypeId: Microsoft.Maps.MapTypeId.road,
    zoom: 19
  };
  //map initialization
  map = new Microsoft.Maps.Map(document.getElementById('mapDiv'), mapOptions);
}
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.watchPosition(success, error, {maximumAge: 5000,
                                                         enableHighAccuracy: true});
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}
function error(err) {
  x.innerHTML = err.toString();
}
function success(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
  var pos = new Microsoft.Maps.Location(position.coords.latitude, position.coords.longitude);
  //console.log(pos);
  p++;
  console.log(p);
  //infobox
  var offsetInfo = new Microsoft.Maps.Point(0, 30); 
  var infoboxOptions  = { title: "Location Information", description:   "This is your current location.", offset: offsetInfo };
  var callout = new Microsoft.Maps.Infobox(pos, infoboxOptions);
  //pushpin
  var offsetPin = new Microsoft.Maps.Point(0, 5);
  var pushpinOptions = {text : '1', visible: true, textOffset: offsetPin};
  var pushpin = new Microsoft.Maps.Pushpin(pos, pushpinOptions);
  map.setView({center:pos});
  if (p <= 1) {
    map.entities.push(pushpin);
  } else {
    pushpin.setLocation(pos);
    console.log(toString(pushpin.getLocation()));
  }
}

【问题讨论】:

  • 您的语言不清楚,并且您没有将其连接到您的代码。请阅读this并采取行动。

标签: javascript geolocation bing-maps


【解决方案1】:

您遇到的问题是您不断创建一个新的 Pushpin 实例并设置它的位置。在第一个图钉之后,没有添加其他图钉。您需要做的是创建一个全局图钉变量,并且仅在变量为空时创建它,如果不是则设置它的位置。这是您的成功函数的修改版本:

var pushpin;

function success(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
    var pos = new Microsoft.Maps.Location(position.coords.latitude, position.coords.longitude);

    p++;
    console.log(p);

    if(pushpin){
        pushpin.setLocation(pos);
    }else{
        //infobox
        var offsetInfo = new Microsoft.Maps.Point(0, 30); 
        var infoboxOptions  = { title: "Location Information", description:   "This is your current location.", offset: offsetInfo };
        var callout = new Microsoft.Maps.Infobox(pos, infoboxOptions);

        //pushpin
        var offsetPin = new Microsoft.Maps.Point(0, 5);
        var pushpinOptions = {text : '1', visible: true, textOffset: offsetPin};
        pushpin = new Microsoft.Maps.Pushpin(pos, pushpinOptions);
        map.entities.push(pushpin);
    }

    map.setView({center:pos});
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    相关资源
    最近更新 更多