2017-02-23 83 views
18

我有工作对离子2项目,到目前为止,我已经实现了地图,但我不能离开这一点的。为了将Google Place和自动完成功能添加到项目中,我需要按照自己的方式进行展示。离子2 - 谷歌的地方,并自动完成位置

我该怎么办?

HTML:

<ion-row> 
    <ion-item> 
    <ion-label>Search</ion-label> 
    <ion-input id="places" type="text" name="search"></ion-input>  
</ion-row> 
<div #map id="map"></div> 

HOME.ts

export class HomePage { 

public latitude: number; 
public longitude: number; 

@ViewChild('map') mapElement; 
map: any; 
marker: any; 
search: string; 

constructor(public navCtrl: NavController, public platform: Platform) { 
/*platform.ready().then(() => { 
    this.InitMap(); 
});*/ 
} 
ionViewDidLoad(){ 
this.InitMap(); 
} 

InitMap() { 

    this.setLocation(); 
    let input = document.getElementById('places'); 
    let autocomplete = new google.maps.places.Autocomplete(input); 

    google.maps.event.addListener(autocomplete, 'place_changed',() => { 

    let place = autocomplete.getPlace(); 
    this.latitude = place.geometry.location.lat(); 
    this.longitude = place.geometry.location.lng(); 
    alert(this.latitude + ", " + this.longitude); 
    console.log(place); 
    }); 

} 

setLocation() { 

    let latLng = new google.maps.LatLng(53.550513, 9.994241); 
    let mapOptions = { 
    center: latLng, 
    zoom: 15, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);  
    this.marker = new google.maps.Marker({ 
    position: latLng, 
    map: this.map, 
    }); 
    } 

} 

有什么不对? 感谢的

+0

究竟是你想和什么反应呢?正如我所说, –

+0

。我想实现地图的自动完成功能。 – Michael

+0

你会得到什么输出? –

回答

18

SOLVED! After a long time I was able to find a solution to my problem. Here's the solution:

的Home.html:

<ion-list> 
    <ion-item> 
    <ion-input (click)="showAddressModal()" [(ngModel)]="address.place"type="text" placeholder="Pick an address">    </ion-input> 
    </ion-item> 
</ion-list> 

Home.ts:

import {Component} from '@angular/core'; 
import {NavController, ModalController} from 'ionic-angular'; 
import {AutocompletePage} from './autocomplete'; 

@Component({ 
    templateUrl: 'build/pages/home/home.html' 
}) 

export class HomePage { 
    address; 

    constructor(
    private navCtrl: NavController, 
    private ModalCtrl:ModalController 
) { 
    this.address = { 
     place: '' 
    }; 
    } 

    showAddressModal() { 
    let modal = this.modalCtrl.create(AutocompletePage); 
    let me = this; 
    modal.onDidDismiss(data => { 
     this.address.place = data; 
    }); 
    modal.present(); 
    } 
} 

AutocompletePage.html:

<ion-header> 
    <ion-toolbar> 
    <ion-title>Enter address</ion-title> 
    <ion-searchbar [(ngModel)]="autocomplete.query" [showCancelButton]="true" (ionInput)="updateSearch()" (ionCancel)="dismiss()"></ion-searchbar> 
    </ion-toolbar> 
</ion-header> 

<ion-content> 
    <ion-list> 
    <ion-item *ngFor="let item of autocompleteItems" tappable (click)="chooseItem(item)"> 
     {{ item }} 
    </ion-item> 
    </ion-list> 
</ion-content> 

AutocompletePage.ts:

import {Component, NgZone} from '@angular/core'; 
import {ViewController} from 'ionic-angular'; 

@Component({ 
    templateUrl: 'build/pages/home/autocomplete.html' 
}) 

export class AutocompletePage { 
    autocompleteItems; 
    autocomplete; 

    latitude: number = 0; 
    longitude: number = 0; 
    geo: any 

    service = new google.maps.places.AutocompleteService(); 

    constructor (public viewCtrl: ViewController, private zone: NgZone) { 
    this.autocompleteItems = []; 
    this.autocomplete = { 
     query: '' 
    }; 
    } 

    dismiss() { 
    this.viewCtrl.dismiss(); 
    } 

    chooseItem(item: any) { 
    this.viewCtrl.dismiss(item); 
    this.geo = item; 
    this.geoCode(this.geo);//convert Address to lat and long 
    } 

    updateSearch() { 
    if (this.autocomplete.query == '') { 
     this.autocompleteItems = []; 
     return; 
    } 
    let me = this; 
    this.service.getPlacePredictions({ input: this.autocomplete.query, componentRestrictions: {country: 'TH'} }, function (predictions, status) { 
     me.autocompleteItems = []; 
     me.zone.run(function() { 
     predictions.forEach(function (prediction) { 
      me.autocompleteItems.push(prediction.description); 
     }); 
     }); 
    }); 
    } 

    //convert Address string to lat and long 
    geoCode(address:any) { 
    let geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address }, (results, status) => { 
    this.latitude = results[0].geometry.location.lat(); 
    this.longitude = results[0].geometry.location.lng(); 
    alert("lat: " + this.latitude + ", long: " + this.longitude); 
    }); 
} 
} 
+0

嗯,这是完美的使用,但它不给经纬度,所以你可以请指导我有拉特朗。 –

+0

Hallo Atam_Jeet Singh,你看到最后一次更新。要获得纬度和长度,您需要将地址转换为地理编码。 – Michael

+0

嗨迈克尔,对我来说这个解决方案不工作,首先我不得不添加'声明让谷歌:任何;'但我仍然有这个错误,当我尝试打开自动完成模态页面TypeError:无法读取未定义的属性'AutocompleteService' '你也有这个错误吗? – Kicker

2

使用elementref访问用来代替document.getElementById 尝试离子电池输入:

<ion-input #places type="text" name="search"></ion-input> 

在你的组件类,

import {Elementref } from '@angular/core'; 
@ViewChild("places") 
    public places: ElementRef; 
    InitMap(){ 
    this.setLocation(); 
    let autocomplete = new google.maps.places.Autocomplete(this.places.nativeElement); 
    google.maps.event.addListener(autocomplete, 'place_changed',() => { 

     let place = autocomplete.getPlace(); 
     this.latitude = place.geometry.location.lat(); 
     this.longitude = place.geometry.location.lng(); 
     alert(this.latitude + ", " + this.longitude); 
     console.log(place); 
     }); 

    } 

检查角文档here

+0

感谢您的回复,但不幸的是它什么也没做。 在控制台我有一个错误:'InvalidValueError:不是HTMLInputElement的一个实例' – Michael

+0

哦..好吧,我会改变我的答案..我认为它的价值 –

+0

伟大的Suraj, 许多错误与'让自动完成=新谷歌。 maps.places.Autocomplete(this.places.nativeElement);'。要显示地图,我需要有'@ViewChild(“地图”)mapElement' – Michael

0

如果你需要的是“谷歌地方对象”,那么你的代码可以非常简单像这样:

<ion-item> 
    <ion-label>Search your city</ion-label> 
    <ion-input formControlName="placeAutofill" id="googlePlaces" required></ion-input> 
</ion-item> 

而且在我的代码:

ionViewWillEnter() { 
    // Google Places API auto complete 
    let input = document.getElementById('googlePlaces').getElementsByTagName('input')[0]; 
    let autocomplete = new google.maps.places.Autocomplete(input, {types: ['geocode']}); 
    google.maps.event.addListener(autocomplete, 'place_changed',() => { 
    // retrieve the place object for your use 
    let place = autocomplete.getPlace(); 
    }); 
} 
相关问题