谷歌地图无法为类属性分配导航器位置
google map unable assign navigator position to class property
这里我使用一个JavaScript类来加载客户端当前位置的地图。从导航地理位置获取结果,但无法将其分配给类属性位置。这是我的类
var GoogleMaps = function(mapID){
var Self = this;
var config = {};
this.mapID = mapID;
this.geocoder = null;
this.mapOptions = null;
this.map = null;
this.infowindow = null;
this.marker = null;
this.useCurrent = false;
this.position = null;
this.latlng = null;
this.init = function(userCurrent){
this.useCurrent = userCurrent;
this.geocoder = new google.maps.Geocoder();
this.mapOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
};
this.infowindow = new google.maps.InfoWindow();
this.map = new google.maps.Map(document.getElementById(this.mapID),this.mapOptions);
this.map.setTilt(45);
if(this.useCurrent){
this.getPosition();
this.setPosition();
}
};
this.getPosition = function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
var currentPosition = position;
console.log(currentPosition);//Working perfect
this.position = position; // Not working
});
} else {
error('Geo Location is not supported');
}
};
this.setPosition = function(){
//console.log('%c '+this.position,"background:orange");
this.latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
};
this.loadMap = function(){
this.map.setCenter(this.latlng);
this.map.setTilt(45);
this.map.setZoom(2);
};
}
var Map = new GoogleMaps("map_canvas");
$(window).load(function(){
Map.init(true);
Map.loadMap();
});
检查类中的getPosition函数
变化
if(this.useCurrent){
this.getPosition();
this.setPosition();
}
和
this.getPosition = function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
var currentPosition = position;
console.log(currentPosition);//Working perfect
this.position = position; // Not working
});
} else {
error('Geo Location is not supported');
}
};
:
if(this.useCurrent){
this.getPosition();
}
和
this.getPosition = function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
var currentPosition = position;
console.log(currentPosition);//Working perfect
this.position = position;
this.setPosition();
});
} else {
error('Geo Location is not supported');
}
};
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 在循环中分配json值时,值被覆盖
- 动态分配GA增强型电子商务跟踪器
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- angular 1.5应用程序中的导航栏
- 无法从jquery Mobile导航栏重定向到另一个页面
- 如何在jQuery中将函数的输出分配给变量
- 为集合分配大量的模型弹药
- 引导程序:在导航栏中,显示悬停在单个位置的基于Li Link的不同内容
- 导航到特定事件的另一个变量页面
- onclick函数需要双击,因为类分配延迟
- MVC 3页面导航和使用javascript传递参数
- 如何在react js中从一个页面导航到另一个页面
- Javascript 将变量分配给警报
- 导航栏没有调整到浏览器屏幕的大小
- 分配左侧的 Javascript 对象括号表示法 ({ 导航 } =)
- 单击导航选项卡时分配jQuery功能
- 谷歌地图无法为类属性分配导航器位置
- 分配一个url子域,自动导航到页面并调用javascript函数