我正在尝试使用带有Angular 2和 -Angular路由器的Nativescript(https://github.com/dapriett/nativescript-google-maps-sdk)的Google Maps插件 . 我只使用Nativescript,但是一旦我添加Angular 2和路由器,我就无法让 Map 显示在UI中 .
由于 <Page>
标签没有't go into an Angular2 component template, I can'使用 <Page>
上的 xmlns:maps="nativescript-google-maps-sdk"
命名空间来实例化 <maps:mapView>
组件基础页面提供了一些指导,但它没有尝试过:https://docs.nativescript.org/ui/ui-with-xml#user-interface-components
有关正确方法的任何想法是?
app.component.ts
import {Component} from "angular2/core";
import {RouteConfig} from "angular2/router";
import {NS_ROUTER_DIRECTIVES, NS_ROUTER_PROVIDERS} from "nativescript-angular/router";
import {HomeComponent} from "./pages/home/home.component";
@Component({
selector: "app-main",
directives: [NS_ROUTER_DIRECTIVES],
providers: [NS_ROUTER_PROVIDERS],
template: "<page-router-outlet ></page-router-outlet>"
})
@RouteConfig([
{ path: "/home", component: HomeComponent, as: "Home", useAsDefault: true },
])
export class AppComponent {
}
home.html的
<stack-layout orientation="vertical">
<label [text]="message"></label>
<mapView [latitude]="latitude" [longitude]="longitude"
[zoom]="zoom" [bearing]="bearing"
[tilt]="tilt" (mapReady)="OnMapReady"
(markerSelect)="onMarkerSelect"
(cameraChanged)="onCameraChanged">
</mapView>
</stack-layout>
home.component.ts
import {Component} from "angular2/core";
var geolocation = require("nativescript-geolocation");
var mapsModule = require("nativescript-google-maps-sdk");
exports.mapView = mapsModule.MapView; //Doesn't work
exports.mapView = new mapsModule.MapView(); //Doesn't work
exports.mapView = mapsModule; //Doesn't work
@Component({
selector: "home",
templateUrl: "pages/home/home.html",
})
export class HomeComponent {
public message:string = "Message set.";
public latitude:number = 30.0;
public longitude:number = -100.0;
public zoom:number = 10;
public bearing:number = 0;
public tilt:number = 0;
constructor() {
this.message = "Home constructed.";
}
OnMapReady(args) { }
onMarkerSelect(args) { }
onCameraChanged(args) { }
}
2 回答
您需要使用
element-registry
API注册mapView
标记名称,例如:https://github.com/NativeScript/nativescript-angular/blob/master/src/nativescript-angular/element-registry.ts#L70-L105
该API尚未记录,但我们应该在未来几周内解决该问题 .
我发现在ns angualr 2中使用这个插件我不得不手动将applicationID添加到root / platforms / adnroid中的build.gradle文件中:
这是在我使用tns插件添加nativescript-google-sdk之后 .
否则应用程序正在成功构建,但部署后无法安装在设备或模拟器上并检查logcat,我发现这很奇怪,因为nativescript不会让你编辑build.gradle文件 . 现在使用其他答案的代码很有效