2016-04-21 9 views
2

Ich benutze angularJS 2 und ich versuche google maps autocomplete folgenden this Guide hinzuzufügen.Integration in angular2 App Google Maps - Place Autocomplete

Ich habe auch Angular-Maps von https://angular-maps.com installiert und in der Lage, eine einfache grundlegende Google-Karte anzuzeigen.

In meiner Datei index.html Ich habe eingefügt:

<script src="https://maps.googleapis.com/maps/api/js?key=xxxxxxx&libraries=places&callback=initAutocomplete"> 

Meine app.ts wie folgt aussieht:

import {Component, provide} from 'angular2/core'; 
import {bootstrap} from 'angular2/platform/browser'; 

import { 
    MapsAPILoader, 
    NoOpMapsAPILoader, 
    MouseEvent, 
    ANGULAR2_GOOGLE_MAPS_PROVIDERS, 
} from 'angular2-google-maps/core'; 

@Component({ 
    selector: 'my-app', 
    directives: [ANGULAR2_GOOGLE_MAPS_DIRECTIVES], 
    template: ` 
    <input id="pac-input" class="controls" type="text" placeholder="Search Box"> 
    <div id="map"></div> 
    ` 
}) 
export class AppComponent 
{ 
    var map = new google.maps.Map(document.getElementById('map'), { 
     center: { lat: -33.8688, lng: 151.2195 }, 
     zoom: 13, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    // Create the search box and link it to the UI element. 
    var input = document.getElementById('pac-input'); 
    var searchBox = new google.maps.places.SearchBox(input); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 

    // Bias the SearchBox results towards current map's viewport. 
    map.addListener('bounds_changed', function() { 
     searchBox.setBounds(map.getBounds()); 
    }); 

    var markers = []; 
    // Listen for the event fired when the user selects a prediction and retrieve 
    // more details for that place. 
    searchBox.addListener('places_changed', function() 
    { 
     var places = searchBox.getPlaces(); 

     if (places.length == 0) { 
      return; 
     } 

     // Clear out the old markers. 
     markers.forEach(function(marker) { 
      marker.setMap(null); 
     }); 
     markers = []; 

     // For each place, get the icon, name and location. 
     var bounds = new google.maps.LatLngBounds(); 
     places.forEach(function(place) 
     { 
      var icon = { 
       url: place.icon, 
       size: new google.maps.Size(71, 71), 
       origin: new google.maps.Point(0, 0), 
       anchor: new google.maps.Point(17, 34), 
       scaledSize: new google.maps.Size(25, 25) 
      }; 

      // Create a marker for each place. 
      markers.push(new google.maps.Marker({ 
       map: map, 
       icon: icon, 
       title: place.name, 
       position: place.geometry.location 
      })); 

      if (place.geometry.viewport) { 
       // Only geocodes have viewport. 
       bounds.union(place.geometry.viewport); 
      } else { 
       bounds.extend(place.geometry.location); 
      } 
     }); 
     map.fitBounds(bounds); 
    }); 
} 

Aber ich bekomme diese Fehlermeldung:

angular2-polyfills.js:332 Error: ReferenceError: google is not 
defined(…)ZoneDelegate.invoke 
@angular2-polyfills.js:332Zone.run 
@angular2-polyfills.js:227(anonymous function) 
@angular2-polyfills.js:576ZoneDelegate.invokeTask 
@angular2-polyfills.js:365Zone.runTask 
@angular2-polyfills.js:263drainMicroTaskQueue 
@angular2-polyfills.js:482ZoneTask.invoke 
@angular2-polyfills.js:434 
+0

Wo können Sie das beheben? – Ari

+1

http://brianflove.com/2016/10/18/angular-2-google-maps-places-autocomplete/ dieser Typ hat es geschafft – deek

Antwort

0

Sie müssen sicherstellen, dass Sie die TypeScript-Deklarationsdateien für Google Maps installiert haben. Ich habe die "googlemaps" DefinitelyTyped-Bibliothek zum Arbeiten gefunden:

$ npm install @types/googlemaps --save-dev 
Verwandte Themen