Я пытался добавить поисковую систему google на боковой навигационной панели, используя Angular 2. Я не могу сделать это.Добавление GoogleLaces SearchBox в другом представлении в Angular 2
Я хочу использовать другой компонент для моего SearchBox как searchBox.component.ts
у меня есть еще один компонент main.component.ts который имеет экземпляр карты, определенный внутри
<div id="mapDiv" ></div>
Я хочу использовать ту же карту внутри searchbox.component.ts, чтобы я мог искать места и добавлять маркер на одну и ту же карту. Как я могу достичь этого?
search.componnt.ts:
import { Component,Output,EventEmitter,OnInit } from '@angular/core';
@Component({
selector:'searchBox',
templateUrl:'searchBox.component.html',
styleUrls:['searchBox.component.css'],
})
declare var google: any;
export class searchBoxComponent implements OnInit{
@Output() searchGeoCode :EventEmitter<any> = new EventEmitter();
@Output() closeButtonClicked: EventEmitter<any> = new EventEmitter();
input:any;
searchBox:any;
ngOnInit():void {
let self= this;
this.input = document.getElementById("pac-input");
this.searchBox = new google.maps.places.Autocomplete(self.input);
this.searchBox.addListener('place_changed', function(){
self.onSearchResultSelect(self);
});
}
onSearchResultSelect(self:any) {
let place:any;
self.places = self.searchBox.getPlace();
if (self.places == "") {
return;
}
place = self.places;
self.map.setCenter(place.geometry.location);
self.searchedGeocode=this.map.getCenter().lat() + "," + this.map.getCenter().lng();
let icon = {
url: "./images/officeMarker.svg",
scaledSize: new google.maps.Size(30,30),
};
self.markernew=new google.maps.Marker({
position: place.geometry.location,
map: self.map,
icon: icon,
title: 'Current Marked Location'
});
}
OnClick() {
this.input.value='';
this.input.focus();
}
}
Мой вопрос заключается в том, как я могу получить доступ к переменной, определенной в карте menu.component.ts в search.component.ts?
menu.component.ts:
initMap(){
this.map = new google.maps.Map(document.getElementById('map'), {
zoom: this.zoom,
center: {lat: this.lat, lng: this.lng},
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT
},
mapTypeControl:false,
streetViewControl:false,
scrollWheelZoom : 'center',
});
}
Заранее спасибо :)
я не получил свой вопрос, он связан с Google мест или получить доступ к переменной, определенной в меню в поиск компонента? –
@ DanyY спасибо за ответ. Я хотел получить доступ к переменной, определенной в menu.component.ts, от search.component.ts – CruelEngine