W dzisiejszym poście pokażę, jak zacząłem realizować punkt MUST 2 (kto nie wie o czym mowa zapraszam do wpisu JourneyPlanner #2 - Moskwa :-)), czyli wyszukiwanie miejsc na mapie Google Maps. Jest to pierwszy wpis z tego cyklu. Na razie stworzyłem podpowiadanie wyszukiwania fraz w najprostszy możliwy sposób tak aby działało i to nie idealnie. W drugiej części mam w planach nanoszenie wyników wyszukiwania na mapę, a następnie refaktoryzację kodu i sposobu prezentowania tego w szablonie.
Wyszukiwarka
Pierwszym elementem, od którego zacząłem była zmiana definicji pobierania biblioteki Google Maps oraz dodanie w nagłówkach aplikacji formularza wyszukiwania. Zostało to wykonane w ten sposób w plikach ./lib/app/index.html i ./lib/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ion-nav-bar class="bar-positive"> | |
<ion-nav-back-button></ion-nav-back-button> | |
<ion-nav-buttons side="left"> | |
<button menu-toggle="left" class="button button-clear"> | |
<i class="icon ion-navicon-round"></i> | |
</button> | |
</ion-nav-buttons> | |
<ion-nav-buttons side="right"> | |
<div class="item-input-inset"> | |
<label class="item-input-wrapper"> | |
<i class="icon ion-ios-search placeholder-icon"></i> | |
<input type="search" id="topBarSearchInput"> | |
</label> | |
<button id="searchButton" class="button button-clear"> | |
<i class="icon ion-ios-search"></i> | |
</button> | |
</div> | |
</ion-nav-buttons> | |
</ion-nav-bar> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://maps.googleapis.com/maps/api/js?libraries=places" async defer></script> |
Kontroler
Kolejną czynnością jaką wykonałem było dodanie kontrolera głównego aplikacji. Dodałem kontroler, który wcześniej w ogóle nie istaniał. Potrzebny jest mi (a przynajmniej w tym momencie tak uważam) do obsługi wstawionego wcześniej formularza. Jako, że chcę aby wpisując dane do pola tekstowego pojawiały się podpowiedzi zgodnie z tym przykładem stworzyłem następujący kontroler:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class AppController { | |
$scope: ng.IScope; | |
$autocomplete : google.maps.places.Autocomplete; | |
constructor(private $injector:ng.auto.IInjectorService, public $scope:ng.IScope) { | |
'ngInject'; | |
this.$injector = $injector; | |
this.$scope = $scope; | |
this.$cordovaGeolocation = this.$injector.get('$cordovaGeolocation'); | |
this.$scope.$on('$ionicView.enter', () => this.onEnter()); | |
} | |
onEnter() { | |
this.$autocomplete = new google.maps.places.Autocomplete( | |
(document.getElementById('topBarSearchInput')), | |
{types: ['geocode']}); | |
} | |
geolocate() { | |
this.$cordovaGeolocation | |
.getCurrentPosition(<ngCordova.IGeolocationOptions>{timeout: 10000, enableHighAccuracy: false}) | |
.then((position) => { | |
var geolocation = { | |
lat: position.coords.latitude, | |
lng: position.coords.longitude | |
}; | |
var circle = new google.maps.Circle({ | |
center: geolocation, | |
radius: position.coords.accuracy | |
}); | |
this.$autocomplete.setBounds(circle.getBounds()); | |
}, (err) => { | |
console.log("unable to find location"); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
import {AppController} from './controller'; | |
// ... | |
// ROUTER CONFIG | |
mod.config(modConfigRouter); | |
mod.controller('AppController', AppController); | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function($stateProvider) { | |
'ngInject'; | |
return $stateProvider.state('app', { | |
abstract: true, | |
views: { | |
'@': { | |
template: require("./index.html"), | |
controller: "AppController as appCtrl" | |
} | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="search" ng-focus="appCtrl.geolocate()" id="topBarSearchInput"> |
Brak komentarzy:
Prześlij komentarz