์๋ ํ์ธ์,
๊ธธ์ฐพ๊ธฐ ์๋น์ค ๋ฅผ ์ง์ํ ๊ณํ์ด ์์ต๋๊น?
์ ์๊ฒ๋ ๊ฐ์ ์ง๋ฌธ..
angular2-google-maps์ ํจ๊ป ๊ธธ์ฐพ๊ธฐ ์๋น์ค๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๊น?
-> ๊ธธ์ฐพ๊ธฐ ์๋น์ค
-> ์
๋น์ ์ ๋
ธ๊ณ ์ ์ง์ฌ์ผ๋ก ๊ฐ์ฌ๋๋ฆฝ๋๋ค
-- ํฌ๋ฆฌ์ค
์ด๊ฒ์ด ๋ด๊ฐ ์ก์ธ์คํ ์ ์๋ ํญ๋ชฉ์ธ์ง ์๊ณ ์ถ์ต๋๋ค. ์ ๋ ์ง๋ ์ค ํ๋๊ฐ ๋ ์์น ์ฌ์ด์์ Google์ ์ ์๋ ๋ฐฉํฅ์ ํ์ํด์ผ ํ๋ ๋ค์ ์ง๋๊ฐ ๋ง์ ํ๋ก์ ํธ๋ฅผ ์งํํ๊ณ ์์ต๋๋ค. ๋ฌธ์๋ฅผ ์ดํด๋ณด์์ง๋ง ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ ์๋ ค์ฃผ์ธ์. ์ด๋ ์ชฝ์ด๋ , ์ด ํ๋ก์ ํธ๋ ํ์์ ์ ๋๋ค! ์์ ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค
์ด ์ง์๋ฌธ์ ๊ทํ์ ์๊ตฌ ์ฌํญ์ ํด๊ฒฐํ ์ ์์ต๋๋ค
import {GoogleMapsAPIWrapper} from 'angular2-google-maps/core/services/google-maps-api-wrapper';
import { Directive, Input} from '@angular/core';
declare var google: any;
@Directive({
selector: 'sebm-google-map-directions'
})
export class DirectionsMapDirective {
@Input() origin;
@Input() destination;
constructor (private gmapsApi: GoogleMapsAPIWrapper) {}
ngOnInit(){
this.gmapsApi.getNativeMap().then(map => {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
directionsService.route({
origin: {lat: this.origin.latitude, lng: this.origin.longitude},
destination: {lat: this.destination.latitude, lng: this.destination.longitude},
waypoints: [],
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
});
}
}
Hey david, ์ง์์ ๊ฐ์ฌํ์ง๋ง ๋ด ์ฝ๋์ ํฌํจ์ํค๋ ๋ฐ ์ด๋ ค์์ ๊ฒช๊ณ ์์ต๋๋ค. ๊ตฌ๊ธ ์ง๋ ๋์ ๋ฐ๋ก ๋ฃ์ด์ผ ํ๋์? ์๋ฅผ ๋ค์ด:
<sebm-google-map sebm-google-map-directions [origin=x] [destination=y][zoomControl]="false" [latitude]="lat" [longitude]="lng" (mapClick)="mapClicked($event)">
</sebm-google-map>
๋๋ ๊ทธ๊ฒ์ ์๋์ํค๋ ๋ฐ ์ด๋ ค์์ ๊ฒช๊ณ ์์ต๋๋ค. ์ ๋ ์ค์ ๋ก Angular 2๋ฅผ ์ฒ์ ์ ํ์ต๋๋ค... ๋์์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
์๋ ํ์ธ์ @ahardworker , ์ด๊ฒ์ด ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์ ๋๋ค:
<sebm-google-map>
<sebm-google-map-directions [origin]="origin" [destination]="destination"></sebm-google-map-directions>
</sebm-google-map>
๊ตฌ์ฑ ์์ ์ปจํธ๋กค๋ฌ ํด๋์ค์์ ์ด ์์ฑ์ ์ ์ํด์ผ ํฉ๋๋ค.
origin = { longitude: 4.333, lattitude: -1.2222 }; // its a example aleatory position
destination = { longitude: 22.311, lattitude: -0.123 }; // its a example aleatory position
๊ฐ์ฌ ํด์! ๊ทธ๊ฒ์ ์๋. ํ์ง๋ง ๋์ ์ผ๋ก ๊ฒฝ์ ์ง๋ฅผ ์ถ๊ฐํ ๋ค์ ํ์ด์ง๋ฅผ ์๋ก ๊ณ ์น์ง ์๊ณ ๋ฐฉํฅ์ ์ ๋ฐ์ดํธํ ์ ์๋์ง ๊ถ๊ธํฉ๋๋ค.
์ค์ ๋ก ์ฌ๊ธฐ์ ๋ด๊ฐ ์ป์ ๊ฒ์ด ์์ต๋๋ค.
๋ด ๊ตฌ์ฑ ์์์ ์ถ๊ฐํ์ต๋๋ค.
@ViewChild(DirectionsMapDirective) vc:DirectionsMapDirective;
๊ทธ๋ฐ ๋ค์ ์ ๊ฒฝ์ ์ง๊ฐ ์ถ๊ฐ๋ ๋๋ง๋ค ๊ตฌ์ฑ ์์์์ vc.updateDirections()๋ฅผ ํธ์ถํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค. ์ง์๋ฌธ์์ ์ฝ๋๋ฅผ ๋ค์ ํธ์ถํฉ๋๋ค. ํ๋ ๊ฒ์ด ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์
๋๊น?
๋ฌผ๋ก @ahardworker , waypoints๋ ์์น์ ๋ฐฐ์ด์ ๋๋ค. ์ง์๋ฌธ์ ๋ ผ๋ฆฌ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ ๋ ฅ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์ด ๋ฐฐ์ด์ ์ฝ์ ํ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด...
<sebm-google-map>
<sebm-google-map-directions [origin]="origin" [destination]="destination" [waypoints]="waypoints"></sebm-google-map-directions>
</sebm-google-map>
์ง๋ น:
@Directive({
selector: 'sebm-google-map-directions'
})
export class DirectionsMapDirective {
@Input() origin;
@Input() destination;
@Input() waypoits;
constructor (private gmapsApi: GoogleMapsAPIWrapper) {}
ngOnInit(){
this.gmapsApi.getNativeMap().then(map => {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
directionsService.route({
origin: {lat: this.origin.latitude, lng: this.origin.longitude},
destination: {lat: this.destination.latitude, lng: this.destination.longitude},
waypoints: waypoints,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
});
}
}
์๋
ํ์ธ์ @davidpestana
Directions-Directive๋ฅผ ํฌํจํ๋ ค๊ณ ํ์ง๋ง ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
Can't bind to 'origin' since it isn't a known property of 'sebm-google-map-directions'.
(์ ์์์ด ๋์์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง ์ด์จ๋ ์ฝ์ ์ ์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.)
HTML
<sebm-google-map [latitude]="lat" [longitude]="lng" [zoom]="zoom">
<sebm-google-map-marker [latitude]="latHome" [longitude]="lngHome" [title]="'titleHome'"</sebm-google-map-marker>
<sebm-google-map-marker [latitude]="latComp" [longitude]="lngComp" [title]="titleComp"</sebm-google-map-marker>
<sebm-google-map-directions [origin]="origin" [destination]="destination"></sebm-google-map-directions>
</sebm-google-map>
์์
`@์ปดํฌ๋ํธ({
์ ํ๊ธฐ: 'google-maps',
์คํ์ผ: [require('./googleMaps.scss')],
ํ
ํ๋ฆฟ: require('./googleMaps.html'),
})
๋ด๋ณด๋ด๊ธฐ ํด๋์ค GoogleMaps {
์๋ ํ: ๋ฒํธ = 48.151839;
lngHome: ๋ฒํธ = 13.831726;
latComp: ์ซ์ = 48.329500;
lngComp: ๋ฒํธ = 14.319765;
titleComp: ๋ฌธ์์ด = "dynatrace";
ํ๋/์ถ์: ์ซ์ = 10;
์๋: ์ซ์ = (this.latComp + this.latHome) / 2;
lng: ์ซ์ = (this.lngComp + this.lngHome) / 2;
์์ = { ๊ฒฝ๋: 4.333, ์๋: -1.2222 };
๋ชฉ์ ์ง = { ๊ฒฝ๋: 22.311, ์๋: -0.123 };
}`
์ง์๋ฌธ์ ์ด๋๊ฐ์ ๋ฑ๋กํด์ผ ํฉ๋๊น?
@ProkerBen ์, ์ฌ์ฉํ๋ ๋ค๋ฅธ ์ง์๋ฌธ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฌ์ฉ ์ค์ธ ๋ชจ๋์ ์ง์๋ฌธ์ ์ ์ธ์ผ๋ก ๋ฑ๋กํด์ผ ํฉ๋๋ค.
์ @ProkerBen , @lazarljubenovic์ด ๋งํ๋ฏ์ด ๋ชจ๋์ ์ง์๋ฌธ์ ๋ฑ๋กํด์ผ ํฉ๋๋ค.
import { CustomMapDirective } from '[your route to directive folder]/google-map.directive';
...
@NgModule({
imports: [...],
exports: [..],
declarations:[...,CustomMapDirective]
})
@davidpestana DirectionsMapDirective๋ ๊ฒฝ๋ก๋ฅผ ํ์ํ์ง๋ง ํ๋/์ถ์=17๋ก ์ค์ ํ์ง๋ง ํ๋๋์ง ์์ต๋๋ค.
Google API ์์ฑ์ DirectionsDisplayService๋ฅผ ์ฌ์ฉํ ๋ ๋ชจ๋ ์จ์ดํฌ์ธํธ๋ฅผ ๋ณด๊ธฐ ์ํด ์ง๋๊ฐ ์๋์ผ๋ก ํ๋/์ถ์๋ฉ๋๋ค. Zoom ๊ฐ์ ์ค์ ํ๋ฉด ๋ฌด์๋ฉ๋๋ค.
@davidpestana ์ผ! ๋๋ GMapSDK์ ์์ํ ๊ธฐ๋ฅ์ ์๋ํ๊ณ ์ ์๋ํฉ๋๋ค.
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setOptions({ preserveViewport: true });
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
DirectionsDisplay.setOptions({ preserveViewport: true }) ์ง๋๊ฐ ํ๋/์ถ์ ๊ฐ์ ์ ์งํ๋๋ก ํฉ๋๋ค.
@davidpestana ์๋ ํ์ธ์,
API์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์ด ์ค๋ ๋์์ ๋ณด์ฌ์ค ๊ฒ๊ณผ ๊ฐ์ ์ง์๋ฌธ์ ์ฌ์ฉํ๊ณ ์์ฃผ ์ ์๋ํฉ๋๋ค!
๋ด๊ฐ ๊ฐ์ง ์ ์ผํ ๋ฌธ์ ๋ ๋์ผํ ์ง๋์ ์ ๊ฒฝ๋ก๋ฅผ ํ์ํด์ผ ํ ๋์
๋๋ค. ์์ Component์์ ๋์ผํ ํจ์๋ฅผ ๋ค์ ํธ์ถํ๊ณ ์ด์ ๊ฒฝ๋ก ์์ ์ ๊ฒฝ๋ก๋ฅผ ์ธ์ํฉ๋๋ค. ์ด๊ฒ์ ๋ด๊ฐ ๊ฐ์ง๊ณ ์๋ ์ฝ๋์
๋๋ค.
`
๋ด๋ณด๋ด๊ธฐ ํด๋์ค DirectionsMapDirective {
@Input() ๋ชฉ์ ์ง :๋ฌธ์์ด;
@Input() ์ถ์ฒ:๋ฌธ์์ด;
constructor (private gmapsApi: GoogleMapsAPIWrapper) {
}
ngOnInit(){
this.setMap();
}
setMap():void{
this.gmapsApi.getNativeMap().then(map => {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer({draggable: true});
directionsDisplay.setMap(map);
directionsService.route({
origin: this.origin,
destination: this.destination,
waypoints: [],
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
});
}
}
`
์ ๊ฒฝ๋ก๋ฅผ ํ์ํด์ผ ํ ๋ ์์ ๊ตฌ์ฑ ์์๊ฐ setMap ํจ์๋ฅผ ํธ์ถํ๊ณ ์ถ๋ฐ์ง ๋ฐ ๋์ ๋ณ์๊ฐ ๋ณ๊ฒฝ๋ฉ๋๋ค.
์์ ์ปดํฌ๋ํธ์์ ๋งต์ ๋ค์ ๋ก๋ํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
@davidpestana ๋๋
@okanok ์ด๊ฑฐ ์์๋์ด?
์๋ ํ์ธ์ @davidpestana ๋ฐ @ahardworker , ์ง์๋ฌธ๊ณผ ์ง์ ๋ HTML david๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ์ง๋ง ๊ณ์ ์ด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. "InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: in property lat: not a number; ๋ฐ ์ ์ ์๋ ์์ฑ ์๋".
Component.ts
์์ = { ๊ฒฝ๋: 4.333, ์๋: -1.2222 }; // ์์์ ์ธ ์ํ ์์น
๋ชฉ์ ์ง = { ๊ฒฝ๋: 22.311, ์๋: -0.123 }; // ์์์ ์ธ ์ํ ์์น
Component.html
<sebm-google-map-directions [origin]="origin" [destination]="destination"></sebm-google-map-directions>
์ค๋ฅ: InvalidValueError: ์์ฑ ์ถ์ฒ: ๋ฌธ์์ด์ด ์๋๋๋ค. LatLng ๋๋ LatLngLiteral์ด ์๋๋๋ค. ์์ฑ์์ lat: ์ซ์๊ฐ ์๋๋๋ค. ๋ฐ ์ ์์๋ ์์ฑ ์๋
์ด๋ค ์์ด๋์ด๊ฐ ์์ต๋๊น?
๊ฐ์ฌ ํด์
@developernm ์๋๋ฅผ ์๋ชป ์ ๋ ฅํ์ต๋๋ค ;)
@bene-starzengruber๋ ์กฐ๊ธ ์๋๋ฅด๊ณ ์์์ต๋๋ค lol
๋๋ ํฐ ๊ด์ฌ์ ๊ฐ์ง๊ณ ์ด ์ฃผ์ ๋ฅผ ๋ฐ๋์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด๊ฒ์ด ํ๋ก์ ํธ(์ฝ์ด)์ ํตํฉ๋๊ธฐ๋ฅผ ๊ธฐ๋ํฉ๋๋ค.
๋๋ ๋ํ ๊ฒฝ๋ก ๊ณํ๊ณผ ๊ธธ ์ฐพ๊ธฐ API๊ฐ ํ์ํ๋ค๋ ๊ฒ์ ์๊ณ ์์ต๋๋ค.
@davidpestana @ahardworker ๊ธธ์ฐพ๊ธฐ ์ง๋๋ฅผ ๊ตฌํํ์ต๋๋ค. ํ์ง๋ง ์ฌ๋ฌ ๋ฐฉํฅ์ ๋ฐ๊ณ ์์ต๋๋ค. ๋ด ์ง๋ ๊ฒฝ๋ก๋ฅผ ์ฒจ๋ถํ์ต๋๋ค. ๋งค๋ฒ ๋จ์ผ ๊ฒฝ๋ก๋ฅผ ์ป๋ ๋ฐฉ๋ฒ์ ์ ์ํ ์ ์์ต๋๊น?
๋ชฉ์ ์ง ๊ฐ์ด ๋ณ๊ฒฝ๋ ๋๋ง๋ค My DirectionsMapDirective ์ง์๋ฌธ์ ํธ์ถํฉ๋๋ค.
๋ฏธ๋ฆฌ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
@okanok @marimuthum17 ,
๋ช ์๊ฐ ๋์ ์๋ํ ํ์ ์ด ๋ฌธ์ ์ ๋ํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ์๋ค๊ณ ์๊ฐํฉ๋๋ค... ์ฌ๊ธฐ์์ ์ ์ญ ๋ฐฉํฅ ํ์๋ฅผ ์ ์ํ๋ ๊ฒ์ด ํ์ํ๋ค๋ ๊ฒ์ ์ฝ์ ์ ์์ต๋๋ค. ์ง์๋ฌธ ๋ด๋ถ์ DirectionsDisplay๋ฅผ ์ ์ํ๋ฉด ๋งค๋ฒ DirectionRenderer์ ์ ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ฏ๋ก ์ด์ ๊ฒฝ๋ก๋ฅผ ์ ๊ฑฐํ ์ ์์ต๋๋ค.
๊ทธ๋์ ๋ด ๊ตฌ์ฑ ์์์ DirectionRenderer๋ฅผ ๋ง๋ญ๋๋ค.
if(this.directionsDisplay === undefined){
this.mapsAPILoader.load().then(() => {
this.directionsDisplay = new google.maps.DirectionsRenderer;
this.showDirective = true;
});
}
๊ทธ๋ฐ ๋ค์ ์ง์๋ฌธ์ @Input() directionsDisplay;
์ฝ์
ํ์ญ์์ค.
๋ณ๊ฑฐ ์๋๊ฑฐ ๊ฐ์๋ฐ.. ํจ๊ณผ๊ฐ ์๋ค์..
@zuschy ์๊ณ ํ์ จ์ต๋๋ค . ๊ทธ๊ฒ์ ๋งค๋ ฅ์ฒ๋ผ ์๋ํฉ๋๋ค!!. ๋์ ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
๋ด๊ฐ ํ ๋ณ๊ฒฝ ์ฌํญ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. @Input() DirectionsDisplay: any; // ์ ๋ ฅ์ ๋ํ ์ ํ์ ์ถ๊ฐํ์ต๋๋ค.
๋น์ ์ ๋ด ํ๋ฃจ๋ฅผ ์ ์ฅํ์ต๋๋ค.
๋๊ตฐ๊ฐ๊ฐ ์ด๊ฒ์ ํ๋ก์ ํธ์ ํตํฉํ๊ธฐ ์ํด ์์ ๋ก ์ธ๋งํผ ์น์ ํ ์ ์์ต๋๊น?
@zuschy , @marimuthum17
๋๋ ์ด์ ๊ฐ์ ์
๋ ฅ์ ํตํด ์ง์๋ฌธ์ DirectionsDisplay๋ฅผ ๋ณด๋ด๋ ค๊ณ ์๋ํ์ง๋ง ์ง์๋ฌธ์์ DirectionsDisplay๋ ํญ์ ์ ์๋์ง ์์์ต๋๋ค.
<sebm-google-map-directions *ngIf="marker_count > 1" [directionsDisplay]="directionsDisplay" [waypointCnt]="waypointCnt" [origin]="origin" [destination]="destination" [waypoints]="waypoints"></sebm-google-map-directions>
๊ตฌ์ฑ ์์์์ ๋๋ ์ด๊ฒ์ํฉ๋๋ค
ngOnInit() {
this.searchControl = new FormControl();
this.setCurrentPosition();
if(this.directionsDisplay === undefined){
this.gmapsApi.getNativeMap().then(map => {
this.directionsDisplay = new google.maps.DirectionsRenderer({
draggable: true,
map: map,
});
});
}
๊ทธ๋ฆฌ๊ณ ์ง์๋ฌธ์์
@Input() directionsDisplay;
ngOnChanges(changes: SimpleChanges) {
console.log('onChange fired');
console.log('changing', changes);
this.gmapsApi.getNativeMap().then(map => {
var directionsService = new google.maps.DirectionsService;
this.directionsDisplay.addListener('directions_changed', function() {
computeTotalDistance(this.directionsDisplay.getDirections());
});
this.directionsDisplay.setMap(map);
directionsService.route({
origin: {lat: this.origin.latitude, lng: this.origin.longitude},
destination: {lat: this.destination.latitude, lng: this.destination.longitude},
waypoints: this.waypoints,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
//directionsDisplay.setDirections({routes: []});
this.directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
Replying to @ssssssssss ใ
ใ
๋๋ ๊ฐ์ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ ์์ต๋๋ค. ์ง์๋ฌธ์์ DirectionsDisplay๊ฐ ์ ์๋์ง ์์ต๋๋ค.
@jjjjjjjjjjjjjjjjr
์ปจํธ๋กค๋ฌ์์ ๋ด ์ง์๋ฌธ์ ์ ์ธํ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
@ViewChild(DirectionsMapDirective) vc: DirectionsMapDirective;
........์๋์ ๊ฐ์ด ์ ๊ทผํฉ๋๋ค.
if(this.vc.directionsDisplay === ์ ์๋์ง ์์){ this.mapsAPILoader.load().then(() => {
this.vc.directionsDisplay = ์๋ก์ด google.maps.DirectionsRenderer;
});
@Bonzysalesman @Giusti @manohar-nyros
๋ด ํ๋ก์ ํธ ์ค ํ๋์ ๋ํด ๊ฐ๋ 2์์ Google์ง๋ ๊ธธ ์ฐพ๊ธฐ ์๋น์ค๋ฅผ ๋ง๋ค์์ต๋๋ค.
์ฌ๊ธฐ์ ์ด๊ฒ์ ๋ํ ๊ธฐ์ฌ๊ฐ ํ๋ ์์ต๋๋ค. ์ ์ฒด ์์ค ์ฝ๋๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.
์ฌ๊ธฐ์ ๋ฐ๋ชจ๊ฐ ์ฒจ๋ถ๋์ด ์์ต๋๋ค. ์ํ๋ ๊ฒฝ์ฐ ์คํฌ๋ฆฝํธ๋ ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค. ๊ทธ๊ฒ์ด ๋ค๋ฅธ ์ฌ๋์๊ฒ๋ ๋์์ด๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
@marimuthum17
ํ ๋ฒ ์ดํด๋ด ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค. ๋ ๋ค๋ฅธ ๋ฌธ์ ๋ ng-for์ ์๋ ์์ฑ ํ๋๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ์
๋๋ค. ๊ทํ์ ์์์ + ๋ฒํผ์ด ์๊ณ ๋ค๋ฅธ ๊ฒ์ ํ๋๊ฐ ๋ํ๋ ๊ฒ์ด๋ผ๊ณ ๋งํ ์ ์์ต๋๋ค. ๊ฒ์ ํ๋๋ #pickupInput / #pickupoutput์ ๋ฐ์ธ๋ฉ๋์ด์ผ ํ๊ณ @Viewchild๊ฐ ์์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ?
@marimuthum17
๋์ ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค. ๊ทธ๊ฒ์ ๋๋ฅผ ์ํด ์ ์๋ํฉ๋๋ค. ๋ค์ ํ๋ฒ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
@marimuthum17 , @zuschy
์ด ์ง์๋ฌธ์ ์ง๊ธ๊น์ง ์ ์๋ํ๊ณ ์์ง๋ง ๋ค๋ฅธ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.
์ฌ์ฉ์๊ฐ ์์ฑ๋ ๊ฒฝ๋ก์์ ๋ง์ปค๋ฅผ ๋๋๊ทธํ ๋ค์ ํด๋น ๊ฒฝ๋ก์ ๋ง์ปค๋ฅผ ์ถ๊ฐํ๋ฉด ๋๋๊ทธํ ๋ง์ปค๊ฐ ์์์ ์ผ๋ก ๋ค์ ์ด๋ํฉ๋๋ค( this.vc.updateDirections())
ํธ์ถํ๊ธฐ ๋๋ฌธ์ ์ง์๋ฌธ์์ "directions_changed"๋ฅผ ๋ค์์ต๋๋ค.
Directive์์ @Output
๋ฅผ ๋ง๋ค๋ ค๊ณ ํ์ง๋ง @Input
๋ฐ @Output
๋ก origin์ ์ฌ์ฉํ ์ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋๋ ๋ํ directionsDisplay
๊ฐ ์์ฑ๋ Component์ ngOnInit
์ ๋ฆฌ์ค๋๋ฅผ ๋ฐฐ์นํ๋ ค๊ณ ์๋ํ์ง๋ง ์ด์จ๋ .addListener
์์ ์ ์๋์ง ์์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ค ๋ง์ปค๊ฐ ๋๋๊ทธ๋์๋์ง ์ ๋ชจ๋ฅด๊ฒ ์ด์ ๋ด ์์ด๋์ด๋ Directions_changed ๋ฆฌ์ค๋์์ ์ ํ ์ผ์ด์ค๋ฅผ ๋ง๋ค๊ณ requestOrigin
๊ฐ ์ถ๋ฐ์ง/๋ชฉ์ ์ง ๋๋ ์จ์ดํฌ์ธํธ ์ค ํ๋์ ์ผ์นํ๋์ง ํ์ธํ๋ ๊ฒ์ด์์ต๋๋ค.
์ฌ๊ธฐ ๋ด ์ฝ๋
์ง๋ น
export class DirectionsMapDirective {
// get variables from map.component
@Input() origin;
@Input() destination;
@Input() waypoints;
@Input() waypointCnt;
@Input() directionsDisplay;
constructor (private gmapsApi: GoogleMapsAPIWrapper) {}
// updateDirections has 2 optional parameters. gets called in map.component
updateDirections(directionsDisplay, o?, d?){
this.gmapsApi.getNativeMap().then(map => {
var directionsService = new google.maps.DirectionsService;
directionsDisplay.addListener('directions_changed', function() {
//this.vc.computeTotalDistance(this.directionsDisplay.getDirections());
if(directionsDisplay && directionsDisplay.dragResult){
let requestOrigin = directionsDisplay.dragResult.request.origin;
let requestDestination = directionsDisplay.dragResult.request.destination;
console.log(this.origin)
if(this.origin && (requestOrigin.lat.toString() == this.origin.latitude && requestOrigin.lng.toString() == this.origin.longitude)){
console.log('if?');
let temp_lat = requestDestination.lat.toString();
let temp_lng = requestDestination.lng.toString();
this.origin.latitude = temp_lat;
this.origin.longitude = temp_lng;
this.vc.updateDirections(directionsDisplay);
}
}
console.log(directionsDisplay);
});
//if origin / destination are undefined use value from optional parameters.
directionsDisplay.setMap(map);
if(!this.destination && d) {
this.destination = d;
}
if(!this.origin && o) {
this.origin = o;
}
// give the route the data, travel mode is driving bc users should plan a camping/ roadtrip.
directionsService.route({
origin: {lat: this.origin.latitude, lng: this.origin.longitude},
destination: {lat: this.destination.latitude, lng: this.destination.longitude},
waypoints: this.waypoints,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function(response, status) {
console.log(status);
if(status == 'OK'){
console.log('new route created')
var legs = response.routes[0].legs;
for (var i = 0; i < legs.length; i++) {
let inputFieldStart = document.getElementById('start') as HTMLInputElement;
let inputFieldDestination = document.getElementById('destination') as HTMLInputElement;
if(inputFieldStart.value == ''){
inputFieldStart.value = legs[i].start_address;
}
if(inputFieldDestination.value == ''){
inputFieldDestination.value = legs[i].end_address;
}
}
if (status === 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
}else{
console.log('keine Route mรถglich!')
}
});
//function for displaying the travel distance
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (var i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total / 1000;
document.getElementById('trip_length').innerHTML = 'The trip is <span id="trip_length_nr">' +total + '</span>km long.';
}
});
}
}
์์:
์ฌ๊ธฐ์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก Mapclicked / Autocomplete์์ this.vc.updateDirections(this.directionsDisplay);
๋ฅผ ํธ์ถํฉ๋๋ค.
๋ด๊ฐ ์ฌ๊ธฐ์์ ๋ค์๊ณผ ๊ฐ์ด ๋ฆฌ์ค๋๋ฅผ ์๋ํ์ ๋:
ngOnInit() {
this.searchControl = new FormControl();
this.setCurrentPosition();
this.mapsAPILoader.load().then(() => {
this.geocoder = new google.maps.Geocoder;
this.directionsDisplay = new google.maps.DirectionsRenderer({draggable: true});
this.directionsDisplay.addListener('directions_changed', function() {
//directionsDisplay = undefined?????
if(this.directionsDisplay && this.directionsDisplay.dragResult){
let requestOrigin = this.directionsDisplay.dragResult.request.origin;
let requestDestination = this.directionsDisplay.dragResult.request.destination;
console.log(this.origin)
if(this.origin && (requestOrigin.lat.toString() == this.origin.latitude && requestOrigin.lng.toString() == this.origin.longitude)){
console.log('if?');
let temp_lat = requestDestination.lat.toString();
let temp_lng = requestDestination.lng.toString();
this.origin.latitude = temp_lat;
this.origin.longitude = temp_lng;
this.vc.updateDirections(this.directionsDisplay);
}
}
console.log(this.directionsDisplay);
});
let autocomplete = new google.maps.places.Autocomplete(this.OriginSearchElementRef.nativeElement, {
types: ["address"]
});
let autocomplete2 = new google.maps.places.Autocomplete(this.DestinationSearchElementRef.nativeElement, {
types: ["address"]
});
let autocomplete3 = new google.maps.places.Autocomplete(this.WaypointSearchElementRef.nativeElement, {
types: ["address"]
});
autocomplete.addListener("place_changed", () => {
this.addAutocomplete(autocomplete, 'origin');
});
autocomplete2.addListener("place_changed", () => {
this.addAutocomplete(autocomplete2, 'destination');
this.vc.updateDirections(this.directionsDisplay);
});
autocomplete3.addListener("place_changed", () => {
this.addAutocomplete(autocomplete3, 'waypoint');
this.vc.updateDirections(this.directionsDisplay);
});
});
}
์ง๋ฌธ:
ํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ชฉ๋ก์ ๊ตฌ๋ ์ค์ ๋๋ค. ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋ง์ปค๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ ๋ ngFor๋ฅผ ์ฌ์ฉํ์ฌ ๋ง์ปค๋ฅผ ๋ง๋ค๊ณ ์ ๊ฑฐํ๊ณ ์์ต๋๋ค. ํน์ ๋ง์ปค์ ๊ธธ์ฐพ๊ธฐ๋ฅผ ์ฐ๊ฒฐํ ๋ค์ ์ง๋์์ ๋ง์ปค๊ฐ ์ ๊ฑฐ๋ ๋ ๊ธธ์ฐพ๊ธฐ๋ฅผ ์ ๊ฑฐํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
๊ธฐ๋ณธ์ ์ผ๋ก ์ฌ์ฉ์๋ ์ฑ์์ ์ฌ์ฉํ ์ ์๋ค๊ณ ๋งํฉ๋๋ค. ๋ง์ปค๋ ์์น์ ๋ํ ๋ฐฉํฅ๊ณผ ํจ๊ป ์ง๋์ ํ์๋ฉ๋๋ค. ๊ทธ๋ค์ด ์ฌ์ฉํ ์ ์๋ค๊ณ ๋งํ ๋. ์ง๋์์ ๋ง์ปค์ ์ฐ๊ฒฐ๋ ๋ฐฉํฅ์ ์ ๊ฑฐํ๊ณ ์ถ์ต๋๋ค.
๋๋ ๋ํ watchPosition์ผ๋ก ์ฌ์ฉ์์ ์์น๋ฅผ โโ๊ด์ฐฐํฉ๋๋ค. ๋ถ๋ช ํ ๋ง์ปค ์์น์ ๋ฐฉํฅ์ด ์ ๋ฐ์ดํธ๋๊ธฐ๋ฅผ ์ํฉ๋๋ค.
@davidpestana ํ๋ฅญํ ์์ ์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค... ๊ธฐ๋ณธ ์ง์๋ฌธ์ ๊ตฌํํ์ง๋ง "ZERO_RESULTS๋ก ์ธํด ๊ธธ์ฐพ๊ธฐ ์์ฒญ์ด ์คํจํ์ต๋๋ค"๋ผ๋ ์ค๋ฅ๊ฐ ํ์๋๋ฉด ์ด ์ค๋ฅ๋ฅผ ํด๊ฒฐํ๋ ๋ฐ ๋์์ด ๋ ๊น์?
๋๋ ๋ํ์ด ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ ์์ต๋๋ค ์ด๊ฒ์ ํ์ธํ์ญ์์ค :
https://github.com/SebastianM/angular-google-maps/issues/985
์ฝ๋์๋ ์๋ฌด๋ฐ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
api๋ฅผ ํธ์ถํ๋ ๋์ Google ์ง๋ ํค๋ฅผ ์ถ๊ฐํ๋์ง ํ์ธํ์ธ์. ๋๋ Google ๋ฌธ์์ ๋ฐ๋ผ ๋งค์ผ ์ ํ ์์ฒญ์ด ์์ต๋๋ค.
@marimuthum17 ๋น์ ์ด ๋ง์ต๋๋ค ๋น์ ์ ์ฝ๋์ ์๋ฌด ๋ฌธ์ ๊ฐ ์์์ต๋๋ค ... ์ฌ์ค ๋๋ placeId์ ์ผ๋ถ๋ฅผ ๋์ณค์ต๋๋ค ... ๋๋ placeId๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ ๊ณตํ์ง ์์์ต๋๋ค. ๊ทธ๊ฒ์ด ๋ฌธ์ ์์ต๋๋ค ...
@MartiniHenry1988 google์ ๊ธธ์ฐพ๊ธฐ ์๋น์ค๋ฅผ ์ฌ์ฉํ์ง ์๊ธฐ ๋๋ฌธ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค....์์ ํ ์๋ฅผ ๋ณด๋ ค๋ฉด @marimuthum17 ์ ์ด์ ๋๊ธ์ ์ฐธ์กฐํ์ธ์.
๋ณ๊ฒฝ ์ฌํญ์ด ์์ต๋๊น? directionsDisplay.setMap(map);
ํ์์ ์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
Argument of type 'GoogleMap' is not assignable to parameter of type 'Map'
@kildareflare @amitdahan ๊ฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ ์ ์์ต๋๋ค. ์์ ์ฌํญ์ ์ฐพ์ ์ ์ด ์์ต๋๊น?
์ ๋ ์ด ๋ฌธ์ ์ ์ง๋ฉดํด ์์ต๋๋ค. @chaoranxie ํด๊ฒฐ์ฑ ์ ์ฐพ์์ต๋๊น?
์ฃ์กํฉ๋๋ค. ์ฃผ์ ๊ฐ ์ด๋ฏธ ์ฝ๊ฐ ์ค๋๋์๋ค๋ ๊ฒ์ ์๊ณ ์์ง๋ง ๋ฌธ์ ๊ฐ ์์ผ๋ฉฐ ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ์์ต๋๋ค.
'sebm-google-map-directions'์ ์๋ ค์ง ์์ฑ์ด ์๋๋ฏ๋ก 'origin'์ ๋ฐ์ธ๋ฉํ ์ ์์ต๋๋ค.
ํ์ง๋ง ์์์ ํด๊ฒฐ์ฑ
์ ์ ์ํ๋๋ฐ ์ด๋ป๊ฒ ํด์ผ ํ๋์ง ์ ๋ชจ๋ฅด๊ฒ ๋๋ฐ ๋๊ตฐ๊ฐ ์๋ฅผ ๋ค์ด์ค ์ ์์ต๋๊น?
๋ด ํ์ฐฎ์ ์์ด ์ค๋ ฅ์ ์ฃ์กํ๋ค๋ ๋ง์์ ๋๋ฆฌ๊ณ ์ถ์ต๋๋ค
@Fabian-Thug ์ด๋ค ๋ฒ์ ์ Angular๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๊น? ์ ๋ 4*์ ์์ผ๋ฉฐ ์ง์๋ฌธ์ด ๋ค๋ฅด๊ฒ ์๋ํฉ๋๋ค.
์ง์๋ฌธ์์ ๊ตฌ๋ฌธ์ ์ ํ๊ธฐ: 'sebm-google-map-directions'์์ ์ ํ๊ธฐ ['sebm-google-map-directions']๋ก ๋ณ๊ฒฝํ ๋ค์ HTML์์ AS ๋์ ์์์ ์ถ๊ฐํฉ๋๋ค(์
์๋ ์์ฑ ์ด๋ฒคํธ ๋ฆฌ์ค๋์ ๋ฌธ์ ๊ฐ ์์ผ๋ฉฐ ๋ฒํผ์ผ๋ก ๋ค์ ์์ฑํ ๊ฒ์ ๋๋ค. ( @marimuthum17 ์ฝ๋ ์์ ์ฌ์ฉ - ๊ทธ์ ์ฌ์ดํธ์์๋ ์๋ํ์ง ์์ง๋ง ๋ฌธ์ ๋ ์ด๋ฒคํธ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
[์
๋ฐ์ดํธ]
์ด๊ฒ์ ๋ณ๊ฒฝํ์ญ์์ค:
์ฅ์: google.maps.places.PlaceResult = autocomplete.getPlace();
์ด์:
const ์ฅ์ = autocomplete.getPlace();
@davidpestana @marimuthum17 ์์ ์์
์ ์ํํ์ต๋๋ค. ์ด์ ์ฌ์ฉ์์ ํ์ฌ ์์น์ ์ ํํ ๋ชฉ์ ์ง ์ฌ์ด์ ๋ฐฉํฅ์ ๋ณด์ฌ์ค๋๋ค.
ํ์ง๋ง ๋ง์ปค ์์ด์ฝ์ ๋ณ๊ฒฝํด์ผ ํฉ๋๋ค! ๊ทธ๋ ๊ฒํ๋ ๋ฐฉ๋ฒ?
๋ฐฉํฅ ๊ฒฐ๊ณผ์ ์ฌ์ฉ์ ์ง์ ๋ง์ปค๋ฅผ ํฌํจํ๋ ๋ฐฉ๋ฒ์ ๋ฌด์์
๋๊น?
@davidpestana
์ด ์๋ฃจ์
์ import { CustomMapDirective } from '[๋๋ ํฐ๋ธ ํด๋๋ก์ ๊ฒฝ๋ก]/google-map.directive'; ํ์ง๋ง ์ด๊ฒ์ ์ํด ์ผ๋ถ ํจํค์ง๋ฅผ ์ค์นํด์ผ ํฉ๋๊น ??
์ค๋ฅ ๋ฐ์ ์ ํ ์ค๋ฅ: ์ ์๋์ง ์์ '์๋ณธ' ์์ฑ์ ์ค์ ํ ์ ์์ต๋๋ค.
@ViewChild(DirectionsMapDirective) vc: DirectionsMapDirective;
๊ณต๊ฐ ์ถ์ฒ : ๋ชจ๋ ;
๊ณต๊ณต ๋ชฉ์ ์ง : ๋ชจ๋ ;
๊ฑด์ค์(
๊ฐ์ธ ๋ชจ๋ฌ ์๋น์ค : NgbModal,
private mapsAPILoader: MapsAPILoader,
private ngZone: NgZone,
private gmapsApi: GoogleMapsAPIWrapper,
private _elementRef : ElementRef
) {}
๊ฐ ์ ๋ฌ
this.vc.origin = { ๊ฒฝ๋: 77.333, ์๋: 12.2222 };
this.vc.destination = { ๊ฒฝ๋: 90.311, ์๋: 28.123 };
this.vc.updateDirections();
๋ฏธ๋ฆฌ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
๋ด ์ง๋์ ๋ ์ง์ ์ด ์์ต๋๋ค. ์ด๋ป๊ฒ ๊ทธ ์ฌ์ด์ ๋ฐฉํฅ์ ์ ์ ์์ต๋๊น?
'@angular/core'์์ { ๊ตฌ์ฑ์์ } ๊ฐ์ ธ์ค๊ธฐ;
'@agm/core'์์ { MouseEvent } ๊ฐ์ ธ์ค๊ธฐ;
@์์({
์ ํ๊ธฐ: '์ฑ ๋ฃจํธ',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
๋ด๋ณด๋ด๊ธฐ ํด๋์ค AppComponent {
ํ๋/์ถ์: ์ซ์ = 8;
์๋: ์ซ์ = 51.673858;
lng: ์ซ์ = 7.815982;
๋ง์ปค: ๋ง์ปค[] = [
{
์๋: 51.673858,
lng: 7.815982,
๋ ์ด๋ธ: 'A',
๋๋๊ทธ ๊ฐ๋ฅ: ์ฐธ
},
{
์๋: 51.373858,
๊ธธ์ด: 7.215982,
๋ ์ด๋ธ: 'B',
๋๋๊ทธ ๊ฐ๋ฅ: ๊ฑฐ์ง
}
]
}
์ธํฐํ์ด์ค ๋ง์ปค {
์๋: ์ซ์;
๊ธธ์ด: ์ซ์;
๋ ์ด๋ธ?: ๋ฌธ์์ด;
๋๋๊ทธ ๊ฐ๋ฅ: ๋ถ์ธ;
}
@davidpestana ๊ทํ์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ ๋ ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
"'sebm-google-map-directions'์ ์๋ ค์ง ์์ฑ์ด ์๋๋ฏ๋ก 'origin'์ ๋ฐ์ธ๋ฉํ ์ ์์ต๋๋ค."
@davidpestana ๊ฒฝ์ ์ง๊ฐ Places API, LatLng ๋ฑ์ ์๋ต์ผ๋ก ์ ์๋ ์ ์๋ค๋ ์ ์ ์ฃผ๋ชฉํ ๊ฐ์น๊ฐ ์์ต๋๋ค.
let waypoints = [
{location: {placeId: place_id}}
];
https://developers.google.com/maps/documentation/javascript/directions#DirectionsRequests
์ด ๋ชจ๋ ๊ฒ์ด ๋๋ฅผ ์ํด ์ผํ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค! ๋ค๋ฅธ ์ง์๋ฌธ๊ณผ ๋์ผํ ํ์์ธ ๊ฒฝ์ฐ agm/core์ ์ถ๊ฐํ ๊ฐ์น๊ฐ ์์ต๋๊น?
์ด์ ๋ํ ์ ๋ฐ์ดํธ๊ฐ ์์ต๋๊น?
origin
๋ฐ destination
๋ฅผ ๋ฌธ์์ด๋ก ์ฌ์ฉํ ์ ์์ต๋๊น?
์ถ์ _time & distance_์ ์ก์ธ์คํ ์ ์๋ ์ด์ ๋ ์ด ์ฝ๋ ๋ธ๋ก์ด ์๋์ ์๊ธฐ ๋๋ฌธ์
๋๋ค. ์ด๊ฒ์ ํด๋์ค์ this.xyz ๋ณ์์ ๋ํ ์ฝ๊ธฐ/์ฐ๊ธฐ ์ก์ธ์ค ๊ถํ์ด ์๋ ์ต๋ช
์ฝ๋ฐฑ ํจ์์
๋๋ค.
}, function(response: any, status: any) {
์๋๋ก ๋ณ๊ฒฝ ํ ํ ํด๋น ๊ธฐ๋ฅ ๋ด๋ถ์ ์ ๋ณด๋ฅผ ์ ์ฅํ๊ณ ์๋น์ค๋ฅผ ์ฌ์ฉํ ์์์์ต๋๋ค. typescript ํด๋์ค์ ๊ณ ์ ํ es6 ํ์ดํ ๊ธฐ๋ฅ์ ์ฌ์ฉํฉ๋๋ค. ์ถ๊ฐ ์ ๋ณด: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
}, (response: any, status: any) => {
์๋
ํ์ญ๋๊น,
๋๊ตฐ๊ฐ๊ฐ ๋ฐฉํฅ ๊ฒฝ๋ก์ ๊ฒฝ๋ก์์ ์ถ๋ ฅ์ผ๋ก ํด๋ฆญ ์์ ์์น์ ๊ฐ์ ์ด๋ฒคํธ๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค ์ ์์ต๋๊น?
๊ฐ์ฌ ํด์
์๋ ํ์ธ์ @davidpestana , @marimuthum17 , @okanok
์ฒ์์ผ๋ก ๊ฒฝ๋ก๋ฅผ ๊ทธ๋ฆด ๋ ์ฌ๋ฐ๋ฅด๊ฒ ํ์๋๋ ๊ฒ๊ณผ ๊ฐ์ ์๋ก์ด ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ ์์ต๋๋ค. ๋ค๋ฅธ ๊ฒฝ๋ก๋ฅผ ๊ทธ๋ฆฌ๋ ค๊ณ ํ๋ฉด ์ฒซ ๋ฒ์งธ ๊ฒฝ๋ก๊ฐ ์ง์์ง์ง ์๊ณ ์ ๊ฒฝ๋ก์ ๊ฒฝ์ฐ ์ ์ด ํ์๋์ง ์์ต๋๋ค.
์ฌ๊ธฐ ๋ด ์ฝ๋๊ฐ ์์ต๋๋ค.
๊ตฌ์ฑ ์์.ts,
showDirections( ) {
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
this.org = { latitude: position.coords.latitude, longitude: position.coords.longitude};
this.dst = {longitude: this.cycle.longitude, latitude: this.cycle.latitude};
console.log('Src ' + this.org.latitude );
console.log('Dst' + this.dst.latitude );
});
}
}
์ด ํจ์๋ ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ํด๋ฆญํ ํ ํธ์ถ๋ฉ๋๋ค. ๋ฐ ๊ตฌ์ฑ ์์์ html,
<agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom">
<appCycleDirection *ngIf="dst !== undefined" [org]="org" [dst]="dst"></appCycleDirection>
<agm-marker [style.height.px]="map.offsetHeight" [latitude]="lat" [longitude]="lng"></agm-marker>
</agm-map>
์ง์์ด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
````
import { ์ง์์ด, ์
๋ ฅ, OnInit } from '@angular/core';
'@agm/core'์์ {GoogleMapsAPIWrapper} ๊ฐ์ ธ์ค๊ธฐ;
let google: any;
@์ง๋ น({
// tslint:disable-next-line:directive-selector
์ ํ๊ธฐ: 'appCycleDirection'
})
๋ด๋ณด๋ด๊ธฐ ํด๋์ค CycleDirectionDirective ๊ตฌํ OnInit {
@Input() ์กฐ์ง;
@์
๋ ฅ() dst;
@Input() originPlaceId: ๋ชจ๋;
@Input() destinationPlaceId: ๋ชจ๋;
@Input() ์จ์ดํฌ์ธํธ: ๋ชจ๋;
@Input() ๋ฐฉํฅํ์: ๋ชจ๋;
@Input() ์ถ์ ์๊ฐ: ๋ชจ๋;
@Input() ์ถ์ ๊ฑฐ๋ฆฌ: ๋ชจ๋;
์์ฑ์(๋น๊ณต๊ฐ gmapsApi: GoogleMapsAPIWrapper) { }
ngOnInit() {
console.log(' ์ง์๋ฌธ์์ ');
console.log(this.org);
this.gmapsApi.getNativeMap().then(์ง๋ => {
const DirectionsService = ์๋ก์ด google.maps.DirectionsService;
const DirectionsDisplay = ์๋ก์ด google.maps.DirectionsRenderer;
DirectionsDisplay.setMap(์ง๋);
DirectionsService.route({
์ถ๋ฐ์ง: {์๋: this.org.latitude, lng: this.org.longitude},
๋ชฉ์ ์ง: {์๋: this.dst.latitude, lng: this.dst.longitude},
์จ์ดํฌ์ธํธ: [],
optimizeWaypoints: ์ฐธ,
์ฌํ ๋ชจ๋: 'TRANSIT'
}, ํจ์(์๋ต, ์ํ) {
if (์ํ === 'ํ์ธ') {
DirectionsDisplay.setDirections(์๋ต);
console.log(์๋ต);
} ๋ ๋ค๋ฅธ {
window.alert('' + ์ํ๋ก ์ธํด ๊ธธ์ฐพ๊ธฐ ์์ฒญ ์คํจ);
}
});
});
}
}
```
๋ด ์ฝ๋์ ๋ฌธ์ ๊ฐ ์๋ ๊ฒฝ์ฐ ์ ๊ฒฝ๋ก๊ฐ ํ์๋ ๋ ์ฒซ ๋ฒ์งธ ๊ฒฝ๋ก๋ฅผ ์ฌ๋ผ์ง๊ฒ ๋ง๋ค๊ณ ์ถ์ต๋๋ค.
์๋
ํ์ธ์, ๋๋ก์ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ๋ฃ์ด๋ ๋ ๊น์?
๊ฐ์ฌ ํด์
๋๋ ๋ด ํ๋ก์ ํธ์์ ๋์ผํ ์ง์๋ฌธ์ ์ฌ์ฉํ๊ณ ์ ์๋ํฉ๋๋ค. ๊ฐ๋จํ ์ง๋ฌธ์
๋๋ค. Waze, Uber ๋๋ lyft ์ด์ ์ง๋์ ๊ฐ์ ๋๋ก์์ ๊ตํต๋(๋นจ๊ฐ์, ๋
ธ๋์ ๋๋ ํ๋์ ๋ถ๋ถ)์ ํ์ํ ์ ์์ต๋๊น?
๋น์ ์ ๋์์ ๋์ด ํ๊ฐํฉ๋๋ค
์ด ์ฌ๋์ ์ด ์ง์๋ฌธ์ ํ๋ฅญํ๊ฒ ์ํํ์ต๋๋ค. ์ ๋ ๊ทธ๊ฒ์ ์๋ํ๊ณ ์์ฃผ ์ ์๋ํฉ๋๋ค. AGM์ ํฌํจ๋ ์ ์๋์ง ๊ณ ๋ คํ์ญ์์ค.
์ด ๋ฌธ์ ๋ ์ต๊ทผ ํ๋์ด ์์๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ค๋๋ ๊ฒ์ผ๋ก ํ์๋์์ต๋๋ค. ๋ ์ด์ ํ๋์ด ์์ผ๋ฉด ํ์๋ฉ๋๋ค. ๊ทํ์ ๊ธฐ์ฌ์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด ์ง์๋ฌธ์ ๊ทํ์ ์๊ตฌ ์ฌํญ์ ํด๊ฒฐํ ์ ์์ต๋๋ค