[英]Ionic 2: Google Map Custom Controls Not Working
I have a google map implementation in Ionic2. 我在Ionic2中有一个Google Map实现。 Map and geolocation works fine.
地图和地理位置很好。 The errors occured only after I tried to implement custom controls/button into the map.
仅当我尝试在地图中实现自定义控件/按钮后,才会发生错误。 I followed this link: https://developers.google.com/maps/documentation/javascript/examples/control-custom
我点击了以下链接: https : //developers.google.com/maps/documentation/javascript/examples/control-custom
This is my js code (Constructor): 这是我的js代码(构造函数):
constructor(navController, platform, app) {
this.navController = navController;
this.platform = platform;
this.app = app;
platform.ready().then(() => {
this.initializeMap();
});
}
Custom control: 自定义控件:
CenterControl(controlDiv, map){
// Set CSS for the control border.
var controlUI = document.createElement('div');
controlUI.style.backgroundColor = '#fff';
controlUI.style.border = '2px solid #fff';
controlUI.style.borderRadius = '3px';
controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
controlUI.style.cursor = 'pointer';
controlUI.style.marginBottom = '22px';
controlUI.style.textAlign = 'center';
controlUI.title = 'Click to recenter the map';
controlDiv.appendChild(controlUI);
// Set CSS for the control interior.
var controlText = document.createElement('div');
controlText.style.color = 'rgb(25,25,25)';
controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
controlText.style.fontSize = '16px';
controlText.style.lineHeight = '38px';
controlText.style.paddingLeft = '5px';
controlText.style.paddingRight = '5px';
controlText.innerHTML = 'Center Map';
controlUI.appendChild(controlText);
// Setup the click event listeners: simply set the map to Chicago.
controlUI.addEventListener('click', function() {
map.setCenter(chicago);
});
}
Map Initialization: 地图初始化:
initializeMap() {
let locationOptions = {timeout: 10000, enableHighAccuracy: true};
navigator.geolocation.getCurrentPosition(
(position) => {
let options = {
// /new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map(document.getElementById("map_canvas"), options);
var centerControlDiv = document.createElement('div');
var centerControl = new CenterControl(centerControlDiv, options);
centerControlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);
},
(error) => {
console.log(error);
}, locationOptions
);
}
My index.html: 我的index.html:
<script src="http://maps.google.com/maps/api/js?key=<MY API KEY>"></script>
The error message given was this: 给出的错误消息是这样的:
Where line 87 refers to: 第87行提到:
var centerControl = new CenterControl(centerControlDiv, options);
I'm unable to set "CenterControl(){}" as "function CenterControl(){}" as I would encounter syntax error in that case. 我无法将“ CenterControl(){}”设置为“ function CenterControl(){}”,因为在这种情况下会遇到语法错误。
I would define CenterControl
like this: 我将这样定义
CenterControl
:
export class CenterControl {
constructor(controlDiv, map){
(...)
}
}
instead of 代替
CenterControl(controlDiv, map) {
(...)
}
and import then when you want to use it... Don't forget the export
keyword. 然后导入,当您要使用它时...不要忘记
export
关键字。
Edit 编辑
If it's a method of your class, simply call it this way: 如果这是您班上的一个方法,只需以这种方式调用它:
this.CenterControl(centerControlDiv, options);
centerControlDiv.index = 1;
this.map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.