简体   繁体   English

如何在Angular应用程序组件中使用Typescript类方法

[英]How to use typescript class method inside Angular app component

I'm totally new to Angular 5 and typescript. 我是Angular 5和Typescript的新手。 I want to use this JS code in my Angular component ( http://jsbin.com/rorecuce/1/edit?html,output ) So, I try to convert it into a typescript class like this(Sorry, I don't know whether it's correct or not, If someone please check and correct me ) 我想在我的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);
  }
}

Inside my app.component.ts , I have a method as getNewRadius(), I want to access above methods. 在我的app.component.ts内部,我有一个名为getNewRadius()的方法,我想访问上述方法。 This is what I tried. 这就是我尝试过的。

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;

}

But I'm getting error [ts] Property 'fromLatLngToPoint' does not exist on type 'typeof MercatorProjection'. 但是我收到错误[ts]类型'typeof MercatorProjection'上不存在属性'fromLatLngToPoint'。

Your method needs 2 parameters 您的方法需要2个参数

 public fromLatLngToPoint(latLng, opt_point) ...

And you are calling it with only one 而你只用一个

var initCoord = MercatorProjection.fromLatLngToPoint(center);
var endCoord = MercatorProjection.fromLatLngToPoint(moved);

Try with this 试试这个

Call it with 2 params 用2个参数调用它

var emptyPoint = new google.maps.Point(0, 0);
var initCoord = MercatorProjection.fromLatLngToPoint(center, emptyPoint);
var endCoord = MercatorProjection.fromLatLngToPoint(moved, emptyPoint);

And declare your method static as you are calling it from the class 并在从类中调用方法时将其声明为静态

public static fromLatLngToPoint(latLng,opt_point) { ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在类位于Angular 6组件内部的应用程序组件上使用[ngClass]? - Is it possible to use [ngClass] on an app component where the class is inside the component for Angular 6? 如何在TypeScript组件中使用Observable中的值 角度4 - How to use value from Observable inside TypeScript component | Angular 4 不确定如何在 angular 组件中使用自定义 TypeScript class - Unsure of how to use custom TypeScript class in an angular component 如何在 angular 6 的子组件中使用服务内部的方法 - How to use a method inside service in a child component in angular 6 如何在Angular2组件中使用导出的类 - How to use an exported class inside an Angular2 component 如何在相同的Angular(Typescript)组件类中从另一个方法调用一个方法? - How to call one method from another method in same Angular (Typescript) Component Class? 如何在Angular 2中使用带有typeScript的bower_component - How to use bower_component with typeScript in Angular 2 angular2如何将回调方法从组件传递到类(typescript文件) - angular2 how to pass callback method from component into class (typescript file) 如何在另一个角组件2中使用一个组件的方法? - How to use method of a component in another component angular 2? How can I use my logo slider JavaScript function inside an Angular component / TypeScript? - How can I use my logo slider JavaScript function inside an Angular component / TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM