繁体   English   中英

整合angular2应用程序谷歌地图 - 放置自动完成

[英]integrate inside angular2 app Google Maps - Place Autocomplete

我正在使用angularJS 2,我正在尝试按照指南添加google maps autocomplete

我还安装了https://angular-maps.com的角度图,并能够显示一个简单的基本谷歌地图。

在我的index.html文件中,我插入了:

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

我的app.ts看起来像这样:

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);
    });
}

但我得到这个错误:

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

您需要确保已安装Google地图的TypeScript声明文件。 我找到了“googlemaps”DefinitelyTyped库来工作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM