I have been trying to add google map to my app using @ionic-native/google-map package but my map is visible only on 4.x Android versions not any other versions. Here is my piece of code.
Have you considered using ElementRef and ViewChild instead of document.getElementById to refer to the map? I think itās always a helpful practice to stick to the frameworkās features to enhance the performance of your appā¦ In theory, you shouldnāt even need document functions in an Ionic app
@Judgewest2000, Iām 100% aware of that, but am not very experienced with ViewChild and accessing the DOM. Essentially, Iām building a map app knowing Iāll have to reverse engineer those weaknesses eventually.
Would you be willing to post a quick example of using ViewChild to access an
?
If you find the time it would be immensely helpful. Iām struggling to put the pieces together in this area.
Iāll owe you 20 bucks.
And honestly, your 2 cents on the subject might be more valueable than an example. I can copy paste all day long. Perhaps your perspective on how ViewChild and that specific element interact would be best.
Good thinkingā¦
I have an app, googlemaps is working, autocomplete is working, google places: working.
But, my code contains things like this:
import {etc..}
export class MapPage {
_sbElement: HTMLInputElement;
constructor() {
}
ionViewDidLoad() {
this._sbElement = document.getElementsByClassName('searchbar-input')[0];
let autoPlaces = new google.maps.places.Autocomplete(this.sbElement);
}
}
Thatās a rough estimate of a working example Iāve put together.
Plus a bunch of other stuff of course.
How would I go about using ViewChild to achieve the same result in a safe manner?
As in, referencing the search bar and passing that reference to autoPlaces, but without document.getElementsByClassName
Iāve got a sleeping baby on me atm so Iāll keep it brief
Two things:
Obviously Ionic does a lot of wrapping of native elements so your mileage may vary as to how successful itāll be using them for this specific case. You might find it easier working with a regular element with only slight scss trickery.
Regardless of the type of element you settle on you you will need to add a hashtag to the elementā¦
<input type="text" #searchbar />
Then to reference it you use the ViewChildā¦
@ViewChild('searchbar') searchBar: ElementRef;
Then to get the element itselfā¦
useElement(){
const element = this.searchBar.nativeElement as HTMLInputElement;
let autoPlaces = new google.maps.places.Autocomplete(element);
}
Definitely a big help. A different perspective was necessary. Mine sucked on this topic I guess. 2 questions.
When you type
@ViewChild('searchbar') searchBar: ElementRef;
Is searchBar: ElementRef; basically the ViewChild version of
word: string; ?
Iām referencing the DOM, but am typing my items in a pretty solid manner. Does remaining vigilant with typing reduce the negative impact of referencing the DOM to a point that using @ViewChild becomes unnecessary or even less necessary?
Sorry, not getting the first questionā¦ do you mean what are you eventually getting back from the ViewChild? If so youāre getting a reference to the HTML element itself. So you can do elementName.innerHTML = āwhateverā;
Second question:
The real world problem is no different than any other kind of reuse issues in any framework. If you put this stuff into a component with an Id or class as a reference, and you end up using that component multiple times like in a loop or whatever, itās game over, youāll select the wrong one etc all the time.
You might think you āneverā will. You will!!! Might not be with this component but with another.
Also keep in mind when you navctrl.push to another page, the previous pageās contents are still in the dom, and thatās a real world obvious problem.
@Judgewest2000, Iāve been able to completely gut my app of direct DOM references using ViewChild and ViewChildren, and made some real progress in combining google maps PlacesService and Autocomplete. So, Thank you again for the chat earlier.
Consider it done, @Judgewest2000. If there is a specific charity youād like to see benefit, let me know. If not, Iāll find one on my own. Great random opportunity to do something good for others.
As I was going through ElementRef in Angular Documentation āhttps://angular.io/api/core/ElementRefā, It was mentioning some security issue.Is it still preferred to access DOM that way?