Using Geocoder with GoogleMapsAPIWrapper
See original GitHub issueI saw in issue #139 you are providing the ability to access google.maps object via the getMap() function, which I assume is now the getNativeMap() function in GoogleMapsAPIWrapper. I’ve also read that other people have gotten it to work, but I can’t find any documentation or examples on how to use the GoogleMapsAPIWrapper and 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);
// });
}
}
Right now I can’t even get the console.log to print out “test”. I’m not sure why. Also would the m variable be the equivalent of google.maps? So that I can then use m.Geocoder()?
I’m also not sure if I’m importing the GoogleMapsAPIWrapper correctly. Currently I’m importing it in the core.module, since the Angular 2 guidelines say to have services be in the core.module. “sebm-google-map” works for me without any issues, so I think the AgmCoreModule is imported correctly, I’m just not sure about how to use 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 }
]
};
}
}
I’ve been able to get google.maps.Geocoder() to work via GMap from PrimeNG, but sometimes I get google is not defined errors. So I’m trying to use Sebastian’s google map instead.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:42
Top GitHub Comments
How about this -
Working example of geocoding service https://stackblitz.com/edit/angular-google-maps-demo-geocoding based on examples listed here.