question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat(map) Autocomplete

See original GitHub issue

I have gathered all the suggestions, tips, and my work together to provide an Autocomplete Solution:

boot:

bootstrap(AppComponent, [
    GOOGLE_MAPS_PROVIDERS,
    provideLazyMapsAPILoaderConfig({
        apiKey: 'yourGoogleKey',
        libraries: ['places'] }),
    ROUTER_PROVIDERS
]);

map html:

  <google-search-bar></google-search-bar>
  <sebm-google-map
    [latitude]="lat"
    [longitude]="lng"
    [zoom]="zoom"
    [disableDefaultUI]="false"
    [streetViewControl] = "false"
    [zoomControl]="true"
    (mapClick)="mapClicked($event)">

      <sebm-google-map-marker *ngFor="let m of markers; let i = index"
        (markerClick)="clickedMarker(i, $event)"
        [latitude]="m.lat"
        [longitude]="m.lng"
        [markerDraggable]="m.draggable"
        [iconUrl]="m.iconUrl"
        (dragEnd)="markerDragEnd($event)">

        <sebm-google-map-info-window
          [isOpen]="m.isOpen">
          <infowindow
            [allEventMarkers]="markers"
            [currentIndex]="i">
          </infowindow>
        </sebm-google-map-info-window>
      </sebm-google-map-marker>
    </sebm-google-map>

search bar:

Then Create an Angular ‘your-search-bar’ component with the following template:

<input type="text" id="your-search-bar" (click)="clearSearch()" class="form-control">

‘YourMapApp’: add the following:

    constructor(private apiLoader: MapsAPILoader, private zone: NgZone) {
    }

    ngOnInit() {
        this.apiLoader.load().then(() => {
            let autocomplete = new google.maps.places.Autocomplete(document.getElementById('your-search-bar'), {});
            google.maps.event.addListener(autocomplete, 'place_changed', () => {
                this.zone.run(() => {
                    let place = autocomplete.getPlace();
                    if (place.geometry.location) {
                        this.lat = place.geometry.location.lat();
                        this.lng = place.geometry.location.lng();
                    }
                });
            });
        });
    }


Working awesome. Feedback please :)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:14
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
JulioWarcommented, Nov 7, 2017

Maybe this will be helpful for someone:

Directive

@Directive({
  selector: '[mapSearch]',
})
export class AppMapSearchDirective {

  @Output() onPlaceChange: EventEmitter<any> = new EventEmitter<any>();

  constructor(private el: ElementRef,
              private ngZone: NgZone,
              private mapsAPILoader: MapsAPILoader) {


    this.mapsAPILoader.load().then(() => {
      const autocomplete = new google.maps.places.Autocomplete(this.el.nativeElement);

      autocomplete.addListener('place_changed', () => {
        this.ngZone.run(() => {
          const place: google.maps.places.PlaceResult = autocomplete.getPlace();
          this.onPlaceChange.emit(place);
        });
      });
    });
  }
}

and then in the html we just need to use it:

<input type="text"
                     mapSearch
                     class="form-control"
                     (onPlaceChange)="placeChanged($event)"
                     id="search_place"/>

It will be awesome if this will add it as a alternative Module that use can import with the library.

0reactions
stale[bot]commented, Nov 13, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Place Autocomplete | Maps JavaScript API - Google Developers
Autocomplete is a feature of the Places library in the Maps JavaScript API. You can use autocomplete to give your applications the type-ahead-search ......
Read more >
feat(map) Autocomplete · Issue #467 - GitHub
I have gathered all the suggestions, tips, and my work together to provide an Autocomplete Solution: boot: bootstrap(AppComponent, [ GOOGLE_MAPS_PROVIDERS, ...
Read more >
use-places-autocomplete - npm
Features ; Provides intelligent places suggestions powered by Google Maps Places API. ; Builds your own customized autocomplete UI by React ...
Read more >
New Autocomplete features in Google Maps Platform help ...
With these new Autocomplete features in Google Maps Platform, your users will be able to find the exact place they're looking for, faster....
Read more >
Address Autocomplete | Maps API - Geoapify
Address Autocomplete API for address inputs, HTML forms, and apps. NPM packages for easy integration and code samples. ... Features and capabilities.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found