[英]Is it possible to use [ngClass] on an app component where the class is inside the component for Angular 6?
[英]How to use typescript class method inside Angular app component
我是Angular 5和Typescript的新手。 我想在我的Angular組件( http://jsbin.com/rorecuce/1/edit?html,output
)中使用此JS代碼,因此,我嘗試將其轉換為這樣的typescript類(對不起,我沒有知道它是否正確,如果有人請檢查並糾正我)
class MercatorProjection {
TILE_SIZE = 256;
pixelOrigin_ : any;
pixelsPerLonDegree_: any;
pixelsPerLonRadian_: any;
bound(value, opt_min, opt_max) {
if (opt_min !== null) value = Math.max(value, opt_min);
if (opt_max !== null) value = Math.min(value, opt_max);
return value;
}
degreesToRadians(deg) {
return deg * (Math.PI / 180);
}
radiansToDegrees(rad) {
return rad / (Math.PI / 180);
}
MercatorProjection() {
let pixelOrigin_ = new google.maps.Point(this.TILE_SIZE / 2, this.TILE_SIZE / 2);
var pixelsPerLonDegree_ = this.TILE_SIZE / 360;
var pixelsPerLonRadian_ = this.TILE_SIZE / (2 * Math.PI);
}
public fromLatLngToPoint(latLng,
opt_point) {
var point = opt_point || new google.maps.Point(0, 0);
var origin = this.pixelOrigin_;
point.x = origin.x + latLng.lng() * this.pixelsPerLonDegree_;
var siny = this.bound(Math.sin(this.degreesToRadians(latLng.lat())), - 0.9999,
0.9999);
point.y = origin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) * - this.pixelsPerLonRadian_;
return point;
}
public fromPointToLatLng(point) {
var origin = this.pixelOrigin_;
var lng = (point.x - origin.x) / this.pixelsPerLonDegree_;
var latRadians = (point.y - origin.y) / -this.pixelsPerLonRadian_;
var lat = this.radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
return new google.maps.LatLng(lat, lng);
}
}
在我的app.component.ts內部,我有一個名為getNewRadius()的方法,我想訪問上述方法。 這就是我嘗試過的。
getNewRadius(){
var numTiles = 1 << this.map.getZoom()
var center =this.map.getCenter();
var moved = google.maps.geometry.spherical.computeOffset(center, 10000, 90); /*1000 meters to the right*/
var projection = new MercatorProjection;
var initCoord = MercatorProjection.fromLatLngToPoint(center);
var endCoord = MercatorProjection.fromLatLngToPoint(moved);
var initPoint = new google.maps.Point(
initCoord.x * numTiles,
initCoord.y * numTiles);
var endPoint = new google.maps.Point(
endCoord.x * numTiles,
endCoord.y * numTiles);
var pixelsPerMeter = (Math.abs(initPoint.x-endPoint.x))/10000.0;
var totalPixelSize = Math.floor(this.desiredRadiusPerPointInMeters*pixelsPerMeter);
console.log(totalPixelSize);
return totalPixelSize;
}
但是我收到錯誤[ts]類型'typeof MercatorProjection'上不存在屬性'fromLatLngToPoint'。
您的方法需要2個參數
public fromLatLngToPoint(latLng, opt_point) ...
而你只用一個
var initCoord = MercatorProjection.fromLatLngToPoint(center);
var endCoord = MercatorProjection.fromLatLngToPoint(moved);
試試這個
用2個參數調用它
var emptyPoint = new google.maps.Point(0, 0);
var initCoord = MercatorProjection.fromLatLngToPoint(center, emptyPoint);
var endCoord = MercatorProjection.fromLatLngToPoint(moved, emptyPoint);
並在從類中調用方法時將其聲明為靜態
public static fromLatLngToPoint(latLng,opt_point) { ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.