gpt4 book ai didi

angular - Google Places API - Angular 2/Typescript/Ionic 2

转载 作者:太空狗 更新时间:2023-10-29 17:52:19 25 4
gpt4 key购买 nike

有没有人把这个转换成 typescript ?

  var map;
var infowindow;

function initMap() {
var pyrmont = {lat: -33.867, lng: 151.195};

map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});

infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: pyrmont,
radius: 500,
type: ['store']
}, callback);
}

function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}

function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});

google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}

我尝试自己转换它,如下所示,但我在控制台中收到一个错误,我将在代码下方显示,如果有人能提醒我哪里出错了,那就太好了

  constructor(public navCtrl: NavController, private navParams: NavParams, private ngZone: NgZone) {}

ionViewDidLoad() {
console.log('ionViewDidLoad DetailsPage');
this.loadMap();
}

@ViewChild('map') mapElement: ElementRef;
map: any;

loadMap(){

Geolocation.getCurrentPosition().then((position) => {

let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}

this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);

let service = new google.maps.places.PlacesService(mapOptions);

service.nearbySearch({
location: latLng,
radius: 500,
type: ['pub']
}, (results, status) => {
this.callback(results, status)
});

}, (err) => {
console.log(err);
});

}

callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
this.createMarker(results[i]);
}
}
}

createMarker(place){
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: this.map,
position: place.geometry.location
});

let infowindow = new google.maps.InfoWindow();

google.maps.event.addListener(marker, 'click', () => {
this.ngZone.run(() => {
infowindow.setContent(place.name);
infowindow.open(this.map,this);
});
});
}

addMarker(){

let marker = new google.maps.Marker({
map: this.map,
animation: google.maps.Animation.DROP,
position: this.map.getCenter()
});

let content = "<h4>You are here!</h4>";

this.addInfoWindow(marker, content);

}

addInfoWindow(marker, content){

let infoWindow = new google.maps.InfoWindow({
content: content
});

google.maps.event.addListener(marker, 'click', () => {
this.ngZone.run(() => {
infoWindow.open(this.map, marker);
});
});
}

控制台错误

Uncaught TypeError: this.b.getElementsByTagName is not a function
at B5.attributionText_changed (places_impl.js:28)
at zb (js?key=AIzaSyAZTATsl9JjVKodXRnzeSk52Ndvtz-HPPU&libraries=places:36)
at B5._.y.bindTo (js?key=AIzaSyAZTATsl9JjVKodXRnzeSk52Ndvtz-HPPU&libraries=places:104)
at Object.f6.f (places_impl.js:38)
at Hw.<anonymous> (js?key=AIzaSyAZTATsl9JjVKodXRnzeSk52Ndvtz-HPPU&libraries=places:137)
at js?key=AIzaSyAZTATsl9JjVKodXRnzeSk52Ndvtz-HPPU&libraries=places:105
at Object.<anonymous> (js?key=AIzaSyAZTATsl9JjVKodXRnzeSk52Ndvtz-HPPU&libraries=places:37)
at js?key=AIzaSyAZTATsl9JjVKodXRnzeSk52Ndvtz-HPPU&libraries=places:105
at js?key=AIzaSyAZTATsl9JjVKodXRnzeSk52Ndvtz-HPPU&libraries=places:37
at js?key=AIzaSyAZTATsl9JjVKodXRnzeSk52Ndvtz-HPPU&libraries=places:105

特别是在控制台文件中突出显示。

b.getElementsByTagName("a"),c=0;c<_.w(b);c++)b[c].style.color="#444";this.H&&this.H.set("placesDataProviders",a)};B5.prototype.hide_changed=function(){_.kA(this.b,!this.get("hide"))};_.t(D5,_.y);_.k=D5.prototype;_.k.input_changed=function(){window.clearTimeout(this.f);this.f=window.setTimeout((0,_.p)(this.Ol,this),100)};_.k.Ol=function(){var a=this.l,b=this.Lb();a!=b&&(H5(this),this.l=b);this.f=null};_.k.Jm=function(){if(this.Tc())J5(this,this.Lb());else{var a={name:this.Lb()};this.Ef(a)}};

最佳答案

如果您添加这样的回调函数,this 上下文将丢失。有多种方法可以解决这个问题。一种是使用匿名函数包装器:

service.nearbySearch({
location: latLng,
radius: 500,
type: ['pub']
}, (results, status) => {
this.callback(results, status);
});

另请注意,当您使用 google.maps 中的 addListener 调用时,不会触发更改检测周期。检查这个answer想要查询更多的信息。您可能会在使用 addInfoWindow 函数时遇到该问题:

@Component({
selector : 'maps-test',
template : '<div #map></div>'
})
export class MapsTestComponent {

@ViewChild('map')
mapElement: ElementRef;

constructor(private ngZone: NgZone){}

ngAfterViewInit() {
loadMap();
}

loadMap(){

Geolocation.getCurrentPosition().then((position) => {

let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}

this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);

let service = new google.maps.places.PlacesService(this.map);


service.nearbySearch({
location: latLng,
radius: 500,
type: ['pub']
}, (results, status) => {
this.callback(results, status)
});

}, (err) => {
console.log(err);
});

}

callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
this.createMarker(results[i]);
}
}
}

createMarker(place){
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: this.map,
position: place.geometry.location
});

let infowindow = new google.maps.InfoWindow();

google.maps.event.addListener(marker, 'click', () => {
this.ngZone.run(() => {
infowindow.setContent(place.name);
infowindow.open(this.map,this);
});
});
}

addMarker(){

let marker = new google.maps.Marker({
map: this.map,
animation: google.maps.Animation.DROP,
position: this.map.getCenter()
});

let content = "<h4>You are here!</h4>";

this.addInfoWindow(marker, content);

}

addInfoWindow(marker, content){

let infoWindow = new google.maps.InfoWindow({
content: content
});

google.maps.event.addListener(marker, 'click', () => {
this.ngZone.run(() => {
infoWindow.open(this.map, marker);
});
});
}
}

关于angular - Google Places API - Angular 2/Typescript/Ionic 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42436268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com