๋ฌธ์ #139์์ getMap() ํจ์๋ฅผ ํตํด google.maps ๊ฐ์ฒด์ ์ก์ธ์คํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๊ฒ์ ๋ณด์์ต๋๋ค. ์ด์ ์ด ํจ์๋ GoogleMapsAPIWrapper์ getNativeMap() ํจ์๋ผ๊ณ ๊ฐ์ ํฉ๋๋ค. ๋๋ ๋ํ ๋ค๋ฅธ ์ฌ๋๋ค์ด ๊ทธ๊ฒ์ ์๋ํ๊ฒ ํ๋ค๋ ๊ฒ์ ์ฝ์์ง๋ง GoogleMapsAPIWrapper ๋ฐ Geocoder๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๋ฌธ์๋ ์์ ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.
import { Component, OnInit } from '@angular/core';
import { LocationService } from '../../core/location.service';
import { GoogleMapsAPIWrapper } from 'angular2-google-maps/core';
declare var google: any;
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
constructor(private wrapper: GoogleMapsAPIWrapper) {
this.wrapper.getNativeMap().then((m) => {
console.log("test");
});
}
ngOnInit() {
// var address = "1045 mission street san francisco";
// var geocoder = new google.maps.Geocoder();
// var result = "";
// geocoder.geocode({ 'address': address }, (results, status) => {
// var latitude = results[0].geometry.location.lat();
// var longitude = results[0].geometry.location.lng();
// console.log("lat: " + latitude + ", long: " + longitude);
// });
}
}
์ง๊ธ์ console.log์์ "test"๋ฅผ ์ถ๋ ฅํ ์๋ ์์ต๋๋ค. ์ด์ ๋ฅผ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๋ํ m ๋ณ์๋ google.maps์ ๋์ผํฉ๋๊น? ๊ทธ๋ฌ๋ฉด m.Geocoder()๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๊น?
๋ํ GoogleMapsAPIWrapper๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ๊ฐ์ ธ์ค๊ณ ์๋์ง ํ์คํ์ง ์์ต๋๋ค. ํ์ฌ Angular 2 ์ง์นจ์์๋ core.module์ ์๋น์ค๊ฐ ์์ด์ผ ํ๋ค๊ณ ๋ช ์๋์ด ์๊ธฐ ๋๋ฌธ์ core.module์์ ๊ฐ์ ธ์ค๊ณ ์์ต๋๋ค. "sebm-google-map"์ ๋ฌธ์ ์์ด ์๋ํ๋ฏ๋ก AgmCoreModule์ ์ฌ๋ฐ๋ฅด๊ฒ ๊ฐ์ ธ์จ ๊ฒ ๊ฐ์ง๋ง GoogleMapsAPIWrapper๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
import {
ModuleWithProviders, NgModule,
Optional, SkipSelf
} from '@angular/core';
import { AgmCoreModule, GoogleMapsAPIWrapper } from 'angular2-google-maps/core';
import { FirebaseService, FirebaseServiceConfig } from './firebase.service';
import { LocationService } from './location.service';
@NgModule({
imports: [AgmCoreModule.forRoot({apiKey: "blahblahapikey"}),],
declarations: [],
exports: [AgmCoreModule],
providers: [FirebaseService, LocationService, GoogleMapsAPIWrapper]
})
export class CoreModule {
constructor( @Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error(
'CoreModule is already loaded. Import it in the AppModule only');
}
}
static forRoot(config: FirebaseServiceConfig): ModuleWithProviders {
return {
ngModule: CoreModule,
providers: [
{ provide: FirebaseServiceConfig, useValue: config }
]
};
}
}
PrimeNG์์ GMap์ ํตํด ์๋ํ๋๋ก google.maps.Geocoder()๋ฅผ ์ป์ ์ ์์์ง๋ง ๋๋๋ก google์ด ์ ์๋์ง ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ๊ทธ๋์ ๋์ Sebastian์ Google ์ง๋๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํฉ๋๋ค.
Geocoder()๋ง ํ์ํ ๊ฒฝ์ฐ getNativeMap()์ ์ฌ์ฉํ ํ์๊ฐ ์์ต๋๋ค.
MapsAPILoader
๋ฅผ ๊ฐ์ ธ์ค๊ณ ์์ผ๋ฏ๋ก ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
this.mapsAPILoader.load().then(() => {
console.log('google script loaded');
var geocoder = new google.maps.Geocoder();
});
๊ทธ๋ฌ๋ ๊ธฐ๋ณธ ์ง๋๋ฅผ ์ป์ผ๋ ค๋ฉด TestComponent๋ฅผ Google ์ง๋ ๊ตฌ์ฑ์์ ๋ด๋ถ์ ๋ก๋ํด์ผ ํฉ๋๋ค.
<sebm-google-map [latitude]="lat" [longitude]="lng">
<app-test></app-test>
</sebm-google-map>
TestComponent
๋ด๋ถ์ GoogleMapsAPIWrapper ์ธ์คํด์ค๊ฐ SebmGoogleMap
์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋์ผํ ์ธ์คํด์ค์ธ์ง ํ์ธํด์ผ ํฉ๋๋ค.
MapsAPILoader ๋ฅผ ์ฌ์ฉํ๋ ํ์ ๋ํด @gnujeremie thx. ์์ ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ๋์? AgmCoreModule๊ณผ ํจ๊ป ๊ฐ์ ธ์ค์ จ๋์?
์๋์, AgmCoreModule์ Google ์ง๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ด ๋ชจ๋์์๋ง ๊ฐ์ ธ์ต๋๋ค.
๋ด ์๋น์ค์์ import { MapsAPILoader } from 'angular2-google-maps/core';
์ ์ง์ ๊ฐ์ ธ์ต๋๋ค.
์ด๊ฑด ์ด๋์ -
import { Injectable, NgZone } from '@angular/core';
import { GoogleMapsAPIWrapper } from 'angular2-google-maps/core';
import { MapsAPILoader } from 'angular2-google-maps/core';
import { Observable, Observer } from 'rxjs';
declare var google: any;
@Injectable()
export class GMapsService extends GoogleMapsAPIWrapper{
constructor(private __loader: MapsAPILoader, private __zone: NgZone) {
super(__loader, __zone);
}
getLatLan(address: string) {
console.log('Getting Address - ', address);
let geocoder = new google.maps.Geocoder();
return Observable.create(observer => {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
observer.next(results[0].geometry.location);
observer.complete();
} else {
console.log('Error - ', results, ' & Status - ', status);
observer.next({});
observer.complete();
}
});
})
}
}
@vintesh ๊ฐ ์ ์ํ ์๋ฃจ์ ์ธ์๋ NgZone ๋ด๋ถ์ ์๋์ ๊ฒฝ๋๋ฅผ ์ ๋ฐ์ดํธํ์ฌ ์ง๋๋ฅผ ์ ๋ฐ์ดํธํด์ผ ํ์ต๋๋ค.
clickSearchAddress() {
this._mapsService.getLatLan(this.model.address)
.subscribe(
result => {
// needs to run inside zone to update the map
this._zone.run(() => {
this.model.lat = result.lat();
this.model.lng = result.lng();
});
},
error => console.log(error),
() => console.log('Geocoding completed!')
);
}
์ง์ค์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ ๋ ์ฌ์ ํ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ฌ๊ธฐ ๋ด ์ฝ๋๊ฐ ์์ต๋๋ค.
import { MapsAPILoader } from 'angular2-google-maps/core';
constructor(public mapsApiLoader: MapsAPILoader) {
this.mapsApiLoader.load().then(() => {
console.log('google script loaded');
this.geocoder = new google.maps.Geocoder();
console.log(this.geocoder);
});
๋ ๊ฐ์ง ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
Cannot find name 'google'.
๊ทธ๋ฆฌ๊ณ
rollup failed: 'MapsAPILoader' is not exported by node_modules/angular2-google-maps/core/index.js (imported
by src/services/location.ts). For help fixing this error see
https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module
@slooker ์์ฑ์ ์ ์ธ ์ ์ declare var google: any;
๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค.
@gnujeremie ์ด๋ Google์ ์ ์๋์ง ์์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ง๋ง ์ฌ์ ํ MapsAPILoader๊ฐ angular2-google-maps์์ ๋ด๋ณด๋ด์ง์ง ์๋๋ค๊ณ ์๋ ค์ค๋๋ค.
@slooker ๋๋ ๊ฐ์ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ "node_modules/angular2-google-maps/core/index.js"๊ฐ MapsAPILoader๋ฅผ ๋ด๋ณด๋ด์ง ์๊ธฐ ๋๋ฌธ์ MapsAPILoader ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด "node_modules/angular2-google-maps/core/index.js" ํ์ผ์ ๋ค์ ์ฝ๋๋ฅผ ์ฝ์ ํฉ๋๋ค.
var core_module_2 = require('./core.umd');
exports.MapsAPILoader = core_module_2.MapsAPILoader;
์ด๊ฒ์ ๋๋ฅผ ์ํด ์๋ํฉ๋๋ค. ์ด๊ฒ์ด ๋น์ ์๊ฒ๋ ํจ๊ณผ๊ฐ ์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
์์ ๋ฒ์ ์ด ์์ต๋๊น? ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋ด ์ฃผ์๋ฅผ ๊ฐ์ ธ์ค๊ณ ๋ง์ปค ํด๋ฌ์คํฐ๋ฅผ ๊ตฌ์ถํ๊ธฐ ์ํด ์ขํ๊ฐ ํ์ํฉ๋๋ค. GeoCoder๋ ์์ฑ์์ ์ ์๋์ด ์์ง๋ง nginit ๋ฉ์๋๋ฅผ ํตํด ์ก์ธ์คํ ์ ์์ต๋๋ค.
VinTesh, ๋์ดํ ์๋น์ค์ Google์ด ์ ์๋์ง ์์๋ค๊ณ ํ์๋๋ ์ด์ ๋ ๋ฌด์์ ๋๊น? ์ด๊ฒ์ ์ค์ ํ๋ ์ ์ ํ ๋ฐฉ๋ฒ์ ๋ฌด์์ ๋๊น? ๊ฐ์ฌ ํด์
๋์ผํ Google ์ ์๋์ง ์์ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ ์์ต๋๋ค. ์ด๋ค ์ ์?
"@agm/core"๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค: "^1.0.0-beta.0"
๊ฐ์ฌ ํด์
getCurrentPositionSuccessCallBack(position: Position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const latLng: LatLngLiteral = {
lat: latitude,
lng: longitude
};
const geoCoder = new google.maps.Geocoder();
geoCoder.geocode({ "location": latLng }, (results, status) => {
๋ด ์ฝ๋์ด๊ณ ์ ์๋ํ์ง๋ง ๋จ์ ํ ์คํธ๋ฅผ ์์ฑํ๋ ค๊ณ ํฉ๋๋ค.
fit('onGeoLocate should set the right location', () => {
spyOn(navigator.geolocation, 'getCurrentPosition').and.callFake(function () {
const position = { coords: { latitude: 32, longitude: -96 } };
arguments[0](position);
});
component.onGeoLocate();
๊ทธ๋ฌ๋ ๊ทธ๊ฒ์ ์คํจํ๋ค
Failed: google is not defined
๋์์ฃผ์ธ์?
์๋
ํ์ธ์ "google undefined" ์น๊ตฌ๋ค์
๋๋ค. ์ฐ๋ฆฌ ๋๋ฃ @gnujeremie์ ํ์ ์๋ ํ์ต๋๊น?
์ด๊ฒ์ ๋๋ฅผ ์ํด ์ผํ๊ณ ์์ต๋๋ค (Angular 2.2.3).
import { Injectable } from '@angular/core';
import { MapsAPILoader } from 'angular2-google-maps/core';
import { Observable } from 'rxjs/Observable';
declare var google: any;
@Injectable()
export class MapsService {
constructor(private __loader: MapsAPILoader) {
}
getGeocoding(address: string) {
return Observable.create(observer => {
try {
//at this point the variable google may be still undefined (google maps scripts still loading)
//so load all the scripts, then...
this.__loader.load().then(() => {
let geocoder = new google.maps.Geocoder();
geocoder.geocode({ address }, (results, status) => {
if (status === google.maps.GeocoderStatus.OK) {
const place = results[0].geometry.location;
observer.next(place);
observer.complete();
} else {
console.error('Error - ', results, ' & Status - ', status);
if (status === google.maps.GeocoderStatus.ZERO_RESULTS) {
observer.error('Address not found!');
}else {
observer.error(status);
}
observer.complete();
}
});
});
} catch (error) {
observer.error('error getGeocoding' + error);
observer.complete();
}
});
}
}
Fabio, ์ด๊ฒ์ด ์ด๋ป๊ฒ ํธ์ถ๋๊ณ ์ฌ์ฉ๋๋์ง์ ๋ํ ์ํ์ ์ ๊ณตํ ์ ์์ต๋๊น?
@DoubleExposure ๋ค์๊ณผ ๊ฐ์ ์์ฑ์๊ฐ ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
์์ฑ์(๋น๊ณต๊ฐ MS: MapsService) {
}
์ฝ๋์์ ์ด๊ฒ์ ํธ์ถํ ์ ์์ต๋๋ค
this.ms.getGeocoding( '10 10th Street NE, Atlanta, GA 30309' ).subscribe(function (x) {
console.log(x.toString());
});
@FabioBentoLuiz @gnujeremie ์๋ ํ์ธ์, ๊ทํ์ ์ฝ๋๋ฅผ ์๋ํ๊ณ ํ๋ฅญํ๊ฒ ์๋ํฉ๋๋ค. ํญ์ ์คํจํ๋ ์ฒซ ๋ฒ์งธ ํธ์ถ์ ์ ์ธํ๊ณ . ngOnInit์์ ์ฒซ ๋ฒ์งธ ํธ์ถ์ ์๋ํ์ง๋ง ์๋ํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
์ฌ๊ธฐ ๋ด ์ฌ์ฉ ์๋
import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { Router, ActivatedRoute, ParamMap} from '@angular/router';
import 'rxjs/add/operator/switchMap';
import { Observable } from 'rxjs/Observable';
import { Place } from './../place';
import { PlaceService } from './../place.service';
import { MapsService } from './map.service';
import { AlertService } from './../AlertComponent/alert.service';
@Component({
selector: 'app-detailview',
templateUrl: './detail.component.html',
styleUrls: ['./detail.component.css'],
})
export class DetailComponent implements OnInit {
place: Place;
lat: number = 51.678418;
lng: number = 7.809007;
ngOnInit() {
let selectedId = this.route.paramMap.subscribe(
(params: ParamMap) => this.place = this.placeService.getPlaceById(+params.get('id'))
);
this.maps.getGeocoding('')
.subscribe(
result => console.log(result),
error => console.log(error),
() => console.log('Geocoding completed!')
);
}
constructor(private placeService: PlaceService,
private route : ActivatedRoute,
private location : Location,
private alert : AlertService,
private maps : MapsService) {
}
clickSearchAddress() {
let address = this.place.name + " " + this.place.address + " " + this.place.city + " " + this.place.postalCode;
this.maps.getGeocoding(address)
.subscribe(
result => {
this.lat = result.lat();
this.lng = result.lng();
},
error => console.log(error),
() => console.log('Geocoding completed!')
);
}
back(){
this.location.back();
}
์๋
ํ์ธ์ @Canadadry๋ ,
์ฒซ ๋ฒ์งธ ํธ์ถ์ด ์ด๋ป๊ฒ ์คํจํฉ๋๊น? MapsAPILoader๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๊น?
๋ด ์ง๋๋ ์ฃผ์๋ฅผ ์ค์ฌ์ผ๋ก ํด์ผ ํฉ๋๋ค.
๊ทธ๋์ ๋ฒํผ์ clickSearchAddress()
๋ฅผ ๋งคํํ๊ณ ๋ ๋ฒ์งธ ํด๋ฆญ์์๋ง ์ง๋๋ฅผ ์ด๋ํฉ๋๋ค. ์ฒซ ๋ฒ์งธ ํด๋ฆญ์๋ ๋ก๊ทธ๋ง ์์ต๋๋ค. ์ง์ค์ฝ๋ฉ์ด ์๋ฃ๋์์ต๋๋ค!
์ด์ํ ์ ์ ngOnInit์ route.paramMap
clickSearchAddress()
ํธ์ถ์ ๋ฃ์๊ณ ์ฒซ ๋ฒ์งธ ํธ์ถ์์ ํจ์๊ฐ ์๋ํ๋ค๋ ๊ฒ์
๋๋ค.
Google ์ง๋ ์คํฌ๋ฆฝํธ๊ฐ ๋ก๋๋์๋์ง ํ์ธํ๊ธฐ ์ํด ํด๋ฆญ์ ์์ํ๊ธฐ ์ ์ ๊ธฐ๋ค๋ฆฌ๋ ค๊ณ ํ์ง๋ง ์ฌ์ ํ ๋ ๊ฐ์ ํด๋ฆญ์ด ํ์ํฉ๋๋ค.
์ฌ์ฉ๋ MapsService๋ ๊ทํ๊ฐ ์ ๊ณตํ ๊ฒ์ ๋๋ค.
์ฌ๊ธฐ ์ ์ค๋ช ๋ ๋๋ก NgZone ์ ์ฌ์ฉํ์ฌ ์๋ํด ๋ณด์ธ์.
import { NgZone } from '@angular/core';
์์ฑ์์ ์ถ๊ฐ
constructor(private _zone: NgZone){
}
๊ทธ๋ฐ ๋ค์ ์๋์ ๊ฒฝ๋๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ค์ ํฉ๋๋ค.
this.maps.getGeocoding(address)
.subscribe(
result => {
this._zone.run(() => {
this.lat = result.lat();
this.lng = result.lng();
});
},
error => console.log(error),
() => console.log('Geocoding completed!')
);
๊ทธ ์ด์ ๋ฅผ ์ค๋ช ํ๋ ๋ ๋ค๋ฅธ ์ค๋ ๋ ๊ฐ ์์ต๋๋ค.
์๋ํ๋์ง ์๋ ค์ฃผ์ญ์์ค.
์๋ํ๋ ๊ฒ ๊ฐ์ง๋ง ์ด์ ๋ฅผ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
๊ฐ์ฌ ํด์
@vintesh , @FabioBentoLuiz ๋ฐ @neilpennell์ ๋ต๋ณ์ ๊ฒฐํฉํ agm์ผ๋ก ์์ ํ์ต๋๋ค.
google-maps.service.ts:
import { Injectable, NgZone } from '@angular/core';
import { GoogleMapsAPIWrapper } from '@agm/core';
import { MapsAPILoader } from '@agm/core';
import { Observable, Observer } from 'rxjs';
declare var google: any;
@Injectable()
export class GMapsService extends GoogleMapsAPIWrapper{
constructor(private __loader: MapsAPILoader, private __zone: NgZone) {
super(__loader, __zone);
}
getLatLan(address: string) {
console.log('Getting Address - ', address);
let geocoder = new google.maps.Geocoder();
return Observable.create(observer => {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
observer.next(results[0].geometry.location);
observer.complete();
} else {
console.log('Error - ', results, ' & Status - ', status);
observer.next({});
observer.complete();
}
});
})
}
}
shop.component.ts:
import { Component, NgZone } from '@angular/core';
import { GMapsService } from '../services/google-maps.service'
@Component({
templateUrl: 'shops.component.html',
styleUrls: ['shops.component.css']
})
export class ShopsComponent {
constructor(private gMapsService: GMapsService, private __zone: NgZone ){}
lat:number
lng:number
getAddress() {
this.gMapsService.getLatLan('Andorra')
.subscribe(
result => {
this.__zone.run(() => {
this.lat = result.lat();
this.lng = result.lng();
})
},
error => console.log(error),
() => console.log('Geocoding completed!')
);
}
}
shop.component.html:
<agm-map [latitude]="lat" [longitude]="lng"></agm-map>
shop.component.css:
agm-map {
height: 300px;
}
๊ณ ๋ง์ ์๋ค์!
@taneljoeaar Google์ด ์ ์๋์ง ์์ ์ด์ ๋ฅผ ์๊ณ ์์ต๋๊น?
MappingService.getLatLan์์
์ ๋ ์ฃผ๋ก ๊ทํ์ ์ฝ๋๋ฅผ ๋ณต์ฌํ์ฌ ๋ถ์ฌ๋ฃ์ต๋๋ค.
์ ๋ง ๊ฐ์ฌํฉ๋๋ค.
@beachjf ๋ @FabioBentoLuiz ์๋ฃจ์ ์ ๋์ ์ฌ์ฉํ์ฌ ์๋์ ๊ฒฝ๋๋ฅผ ๊ตฌํฉ๋๋ค. Google ์ง๋ ์คํฌ๋ฆฝํธ๊ฐ ์์ง ๋ก๋ ์ค์ผ ๋ ๋ณ์ " google"์ด ์ฌ์ ํ ์ ์๋์ง ์์์ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค. ๊ทธ๊ฒ์ ๋๋ฅผ ์ํด ์๋ํฉ๋๋ค.
๋
ธ๋ ฅํ๋ค
'@agm/core'์์ { GoogleMapsAPIWrapper } ๊ฐ์ ธ์ค๊ธฐ;
๋์ ์
'angular2-google-maps/core'์์ { GoogleMapsAPIWrapper } ๊ฐ์ ธ์ค๊ธฐ;
์ต์ ๊ฐ๋
@taneljoeaar ๋๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ ํ์ผ์์ import { } from 'googlemaps';
๋ฅผ ์๋ํ์ญ์์ค.
googlemaps ์ ํ์ด ์ค์น๋์ด ์๋์ง ํ์ธํ์ญ์์ค. ( npm install --save @types/googlemaps
)
๊ธ์, ๊ทธ๊ฒ์ด ์ค๋๋ ์ง๋ฌธ์ด๋ผ๋ ๊ฒ์ ์๊ณ ์์ง๋ง ์ด ์ฃผ์ ์ ๋ํด ๋์, ์ฃผ ๋ฐ ๊ตญ๊ฐ์ ์ด๋ฆ๊ณผ ์ ํ์ด ์๋ ์์น ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ์ ๋ํ ์ฝ๋์ ๋ํด ์ค๋ช ํ๊ณ ์ถ์ต๋๋ค. ๊ทธ๋ฌ๋ ์ ๊ฒฝ์ฐ์๋ ์ฌ์ฉ์ ์ขํ์์ ์ด๋ฌํ ์ด๋ฆ์ ๊ฐ์ ธ์์ผ ํ์ต๋๋ค. ๊ทธ๋์, ๋๋ ๋ง์ง๋ง ์ฝ๋ฉํธ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ด๊ฒ์ ์ผ์ต๋๋ค.
import {MapsAPILoader} from '@agm/core';
...
constructor(private mapsAPILoader: MapsAPILoader) {
// Get this 'res' from some source, like ionic native geolocation
// Or, for test, create some object like:
let res = {
coords: {
latitude: 40.826514,
longitude: -73.914628
}
}
this.codeLatLng(res.coords);
}
...
codeLatLng(coords) {
this.mapsAPILoader.load().then(() => {
console.log('google script loaded');
let latlng = new google.maps.LatLng({lat: coords.latitude, lng: coords.longitude});
let geocoder = new google.maps.Geocoder();
let location = {
country: null,
state: null,
city: null
};
geocoder.geocode({
'latLng': latlng
}, (results, status) => {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
for (let i = 0; i < results[0].address_components.length; i++) {
for (let b = 0; b < results[0].address_components[i].types.length; b++) {
if (results[0].address_components[i].types[b] == "country") {
location.country = !location.country ? results[0].address_components[i] : location.country;
} else if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
location.state = !location.state ? results[0].address_components[i] : location.state;
} else if (results[0].address_components[i].types[b] == "administrative_area_level_2") {
location.city = !location.city ? results[0].address_components[i] : location.city;
}
if (location.city && location.state && location.country) {
break;
}
}
}
console.log(location);
} else {
console.log("Results not available");
}
}
else {
console.log("Geocoder failed due to: ", status);
}
});
}
);
}
์ขํ ๊ฐ์ ์ป์ผ๋ ค๋ฉด ์ด์จ ๊ธฐ๋ณธ ์์น ์ ๋ณด๋ฅผ ์ฌ์ฉํ์ต๋๋ค.
์ฝ๋๋ Angular 2+ ๋ฐ Ionic ํ๋ก์ ํธ์์ ๊ฐ์ ธ์์ต๋๋ค.
๋ณด์ฌ์ฃผ์ ๋๋ก ์ฝ๋๋ฅผ ๊ตฌํํ์ง๋ง ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ค๋ฅ ์ค๋ฅ: StaticInjectorError [MapsAPILoader]:
StaticInjectorError [MapsAPILoader]:
NullInjectorError: MapsAPILoader์ ๋ํ ๊ณต๊ธ์๊ฐ ์์ต๋๋ค!
_NullInjector.get์์(core.js: 923)
resolveToken์์(core.js: 1211)
tryResolveToken์์(core.js: 1153)
StaticInjector.get์์(core.js: 1024)
resolveToken์์(core.js: 1211)
tryResolveToken์์(core.js: 1153)
StaticInjector.get์์(core.js: 1024)
resolveNgModuleDep(core.js: 10586)
_createClass์์(core.js: 10625)
_createProviderInstance $ 1์์ (core.js: 10597)
์ฐธ๊ณ : ์ ๋ Angular 5๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
์ ์๊ฒ ์์ ๋ด๋ฐ์ด ์ฃผ์๊ฒ ์ต๋๊น?
๊ฐ์ฌํฉ๋๋ค.
@ronaldrenteria ๋ฐฐ์นํ์ จ๋์?
AgmCoreModule.forRoot({
apiKey: '<YOUR-GOOGLE-API-KEY>'
})
app.modules.ts์ "๊ฐ์ ธ์ค๊ธฐ"์ ๋ํด?
LazyLoading์ ์ฌ์ฉํ๊ณ ์์ต๋๊น?
์ด ์ค๋ฅ๋ ์ผ๋ถ ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์ํํ๋ ๊ฒ์ ์๊ฑฐ๋ lib๊ฐ ์๋ํ๋ ๋ฐ ํ์ํ ๋ชจ๋ ์๊ตฌ ์ฌํญ์ ์ํํ๋ ๊ฒ์ ์์์ ๋ ๋ฐ์ํฉ๋๋ค.
Angular Maps - ์์ํ๊ธฐ ํ์ด์ง์์ ๋ ๋ง์ ์ ๋ณด๋ฅผ ์ป์ ์ ์์ต๋๋ค.
๊ธ์์, ์ ๋ ์ด๋ฏธ ์ง๋์ ๋ง์ปค๋ฅผ ๋ณด์ฌ์ฃผ๊ณ ์์ง๋ง ์ ๊ฐ ํ๊ณ ์ถ์ ๊ฒ์ ์ผ๋ถ ๋ฐฉํฅ์ ์๋์ ๊ฒฝ๋๋ก ๋ณํํ๋ ๊ฒ์ ๋๋ค. ํ์ง๋ง ์ด๊ฒ์ ๋ถ๊ฐ๋ฅํ์ต๋๋ค.
๊ฐ ๊ตฌ์ฑ ์์๊ฐ ์์ฒด ๋ชจ๋์ ์ฌ์ฉํ๊ณ ์๊ณ LazyLoading์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ app.modules.ts์ ๊ฐ์ ธ์ค๊ธฐ๊ฐ ์์ต๋๋ค.
์ด ๊ฒฝ์ฐ home.component.ts๋ผ๋ ๊ตฌ์ฑ ์์๊ฐ ์๊ณ ์ฌ๊ธฐ์๋ home.module.ts๋ผ๋ ์์ฒด ๋ชจ๋์ด ์์ต๋๋ค.
MapsAPILoader๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ ์ด๋ฏธ ์ง๋์ ๋ง์ปค๋ฅผ ํ์ํ์ง๋ง(์: ๊ฒฝ๋์ ์๋๋ฅผ ์๋์ผ๋ก ๋ฐฐ์นํ์ฌ) ์ด์ DB์์ ์ฃผ์๋ฅผ ์ฝ๊ณ ๊ฒฝ๋ ์๋๋ฅผ ์ป๊ณ ์ถ์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค.
maps.service.ts
import { Injectable, NgZone } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
// Manejo de Mapas
import { GoogleMapsAPIWrapper, MapsAPILoader } from '@agm/core';
import { HttpClientModule } from '@angular/common/http';
import { HttpModule } from '@angular/http';
declare var google: any;
@Injectable()
export class MapsService extends GoogleMapsAPIWrapper {
constructor(private __loader: MapsAPILoader, private __zone: NgZone) {
super(__loader, __zone);
}
getLatLan(address: string) {
console.log('Getting Address - ', address);
let geocoder = new google.maps.Geocoder();
return Observable.create(observer => {
geocoder.geocode({ address: address }, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
observer.next(results[0].geometry.location);
observer.complete();
} else {
console.log('Error - ', results, ' & Status - ', status);
observer.next({});
observer.complete();
}
});
});
}
}
home.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { homeRouting } from './home.routing';
import { SmartadminModule} from '../shared/smartadmin.module';
import { HomeComponent} from './home.component';
// 614iT
import { config_church } from './../shared/churchpro.config';
// Indispensable para manejar los mapas.
import { AgmCoreModule, MapsAPILoader } from '@agm/core';
@NgModule({
imports: [
CommonModule,
homeRouting,
SmartadminModule,
AgmCoreModule.forRoot({
apiKey: config_church.GOOGLE_API_KEY
})
],
declarations: [HomeComponent]
})
export class HomeModule {}
home.component.ts
import { Component, OnInit, NgZone } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { HttpModule } from '@angular/http';
// Mapas
import { MapsService } from '../shared/services/maps.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: []
})
export class HomeComponent implements OnInit {
title: string = 'My first AGM project';
lat: number = 4.6517056;
lng: number = -74.1028404;
zoom: number = 13;
Glat: number;
Glng: number;
constructor(private _mapsService: MapsService, private __zone: NgZone) {}
ngOnInit() {}
getAddress() {
this._mapsService.getLatLan('Andorra').subscribe(result => {
this.__zone.run(() => {
this.Glat = result.lat();
this.Glng = result.lng();
});
}, error => console.log(error), () => console.log('Geocoding completed!'));
}
}
์๋ ํ์ธ์ ์ฌ๋ฌ๋ถ
๋๊ตฐ๊ฐ ๋น์ทํ ๋ฌธ์ ์ ์ง๋ฉดํด ์์์ง๋ ๋ชจ๋ฆ ๋๋ค.
AGM ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉ์ ์ ์ํด์ผ ํฉ๋๋ค. ์ฝ๋๋ฅผ ๋ค์ด๋ก๋ํ๊ณ ์ด ์ฝ๋๋ฅผ ๋ด ํ๋ก์ ํธ์ ์ผ๋ถ์ฒ๋ผ ํฌํจํ์ผ๋ฏ๋ก ๋ค์๊ณผ ๊ฐ์ด apps.module์ ๋ก์ปฌ src ํด๋์์ agmCoreModule์ ์ถ๊ฐํฉ๋๋ค.
import { AgmCoreModule } from '../agmLocalModule/core.module';
AgmCoreModule.forRoot({
API ํค: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}),
ngFor ๋ฃจํ์์ ์์ฑ๋ ๋ง์ปค๊ฐ ์๋ ํ์ด์ง๊ฐ ์์ต๋๋ค. ํ์ง๋ง ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
```
[๊ฒฝ๋]="ubicacion.posicion.longitud"
[markerClickable]="์ฐธ"
>
```
์ค๋ฅ ์ค๋ฅ: ์กํ์ง ์์(์ฝ์ ์ค): ReferenceError: Google์ด ์ ์๋์ง ์์์ต๋๋ค.
ReferenceError: Google์ด ์ ์๋์ง ์์์ต๋๋ค.
MarkerManager.webpackJsonp.102.MarkerManager.addMarker์์ (marker-manager.ts:82)
AgmMarker.webpackJsonp.291.AgmMarker.ngOnChanges(marker.ts:169)
๋ฌธ์ ๊ฐ ๋ ์ ์๋ ๋ชจ๋ ์์ด๋์ด
'google is undefined' ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ๋ชจ๋ ์ฌ๋๋ค์ ์ํด:
agm-map ์์์ ์ด ์ด๋ฒคํธ 'mapReady'๋ฅผ ์ฌ์ฉํฉ๋๋ค.
" https://angular-maps.com/api-docs/agm-core/components/AgmMap.html#mapReady "
๋๊ตฐ๊ฐ๊ฐ ์ค์ ์ฌ๋ก๋ฅผ ์๊ฐํด ๋ผ ์ ์์ต๋๊น?
์ฌ๊ธฐ์ ๋์ด๋ ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ ์ง์ค์ฝ๋ฉ ์๋น์ค https://stackblitz.com/edit/angular-google-maps-demo-geocoding ์ ์ค์ ์.
mapsAPILoader.load()๋ฅผ ์ฌ๋ฌ ๋ฒ ํธ์ถํ ์ ์์ต๋๊น?
์๋ ํ์ธ์ ์ฌ๋ฌ๋ถ, ์ ๋ ์ฝ๊ฐ์ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค. AGM๊ณผ ๊ฐ๋๋ ์ฒ์์ด๊ฑฐ๋ ์. ํ์ง๋ง ์ ๋ place_id๋ฅผ ์ป์์ต๋๋ค. ์ด๊ฒ์ latlng ๊ฐ์ฒด๋ก ๋ณ๊ฒฝํ๊ณ ์ถ์์ต๋๋ค. ๋์์ด ํ์ํฉ๋๋ค.
@henoktsegaye ๊ธฐ๋ณธ ์์น์ ์ฌ๊ธฐ https://developers.google.com/maps/documentation/javascript/examples/geocoding-place-id
๋ฐ๋ผ์ ์์ ๊ฒ์๋ ์์ ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ช ๋ง๋๋ก this.geocoder = new google.maps.Geocoder()
๋ฐ geocoder.geocode({'placeId': placeId}, callbackFunction(results, status))
๊ฐ ํ์ํ์ง๋ง ์ด๊ฒ์ ์คํฌ๋ฆฝํธ๊ฐ ๋ก๋๋ ํ์
๋๋ค.
์ด ๋ฌธ์ ๋ ์ต๊ทผ ํ๋์ด ์์๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ค๋๋ ๊ฒ์ผ๋ก ํ์๋์์ต๋๋ค. ๋ ์ด์ ํ๋์ด ์์ผ๋ฉด ํ์๋ฉ๋๋ค. ๊ทํ์ ๊ธฐ์ฌ์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
@supruniuk thz GOD
Observable ๊ฒฐ๊ณผ๋ฅผ ์ ๋ ฅํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
@xanscale ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ ์ ํ ๊ฒ์ฌ์ ์ฌ์ฉํ์ญ์์ค.
๋ด๋ณด๋ด๊ธฐ ์ธํฐํ์ด์ค ์์น {
์๋: ์ซ์;
๊ธธ์ด: ์ซ์;
}
@supruniuk ๊ฒฐ๊ณผ ๋ฐ ์ํ์ ๊ฐ์ Google ๊ฐ์ฒด๋ฅผ ์ ๋ ฅํ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
@SainiSK ๋๊ธ๋ก
'google is undefined' ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ๋ชจ๋ ์ฌ๋๋ค์ ์ํด:
agm-map ์์์ ์ด ์ด๋ฒคํธ 'mapReady'๋ฅผ ์ฌ์ฉํฉ๋๋ค.
" https://angular-maps.com/api-docs/agm-core/components/AgmMap.html#mapReady "
๊ฒฌ๋ณธ:
(mapReady)="initializedGoogleMapGeocoder($event)"
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด๊ฑด ์ด๋์ -