Angular Google maps confused (doc) loading maps - angularjs

I am new in Angular and Maps, but however would like to try the Angular Google Maps.
When reading the quickstart I am confused at the 3rd point
:
Include the Google Maps API v3, via one of two ways:
Google Maps SDK Async Loader New in v2.0.0
Google indicates using
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?
key=YOUR_API_KEY&callback=initMap">
The quiqkstart proposes also the second option:
Directly load into your HTML page. Example:
<script src='//maps.googleapis.com/maps/api/js?sensor=false'></script>
If the second option is clear, the first confuses me.
Is the difference between first and second option just adding async defer to the script tag?
Where the API key should be used? in the config of Angular Module or in the google maps script?
PS.
The code sample the dev team gave as example didn't clarify either this question, because they didn't use any key at all for the maps initialization...

In the first case you provide a callback (initMap), which will get called, as soon as the map-script is loaded asyncronous. You can configure the map programmatically.
In the second case the script will be loaded and can be used by angular-google-maps later. You have to configure the map over the url.

The difference is with the API key, your account can be bound to additional libraries from within the google console. You require an API key and additional libraries to your liking (directions, geocoding, e...) for certain domains.
The call to your script resource can be kept simple.
The libraries have to be managed within the console.
Without an API key you can load gm with additional libraries as well. But your domain is limited to an amount of request / day.
//maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry&callback=asyncGoogleMaps
If you simply use a script tag in the head section of your page, there's almost no issue in loading the library.
But if you work asynchronously, you'll have to create a global function to map the script initialisation. Else it won't work since the script can't write to the DOM.
If you need to pass in an API key to the javascript, you can set a scope variable in your controller
$scope.googleMapsUrl="http://maps.google.com/maps/api/js?v=3.20&client=XXXXenter-api-keyXXXX";
This can be set from a constant value in your app to standardise the API key to pass to google for multiple controllers.
<div map-lazy-load="http://maps.google.com/maps/api/js"
map-lazy-load-params="{{googleMapsUrl}}">
<map center="41,-87" zoom="3"></map>
</div>
As read on angular-js-googlemaps.

Related

How to use google map API_KEY in angular-google-maps 2.2.1

Recently I am working on a mobile app project which has been developed back in 2016.
Around 4500 users are currently using it.
It was developed using AngularJS (v 1.7.8) and IONIC (V1.1.1). We have to stick with these older version for now.
It has a google map feature.
It is done by first calling "geocode" funtionality of google map java script API and then we use "ui-gmap-google-map" directive from "angular-google-maps 2.2.1 to render the map.
Now it started giving error.
We have to use google map API_KEY now.
To get the geocode the app is making a call to this google map API.
https://maps.google.com/maps/api/geocode/json?address=
Now we have to append an API_KEY in the URL to make it work.
After getting geocode (lat and lng), we used to pass it to "ui-gmap-google-map" angular directive ("center" attribute) in the view to render the map. I am guessing this "ui-gmap-google-map" is internally making calls to google map service and passing the geocode and then render the map.
The map which is being displaying is covered with gray color and water mark "For development purpose only".
Now here the question is, how I can pass the same API_KEY to "ui-gmap-google-map" ?
Please help or show me some direction.
Thanks a lot.
Bibhudatta
You should switch to angularjs-google-maps
If you need to pass in an API key to the javascript, you can set a scope variable in your controller (e.g. $scope.googleMapsUrl="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY_HERE";). This can be set from a constant value in your app to standardise the API key to pass to google for multiple controllers.

Storing API keys in MEAN Stack

Looking for the best way of storing(securely) a Google Places API key in a MEAN stack. I was going to use a .env file but I am not sure how to go about implementing this. My current solution is having express pull in the .env variable but then I am not sure what route to take from there to insert the key into the script tag. I can't find anything from angular on how to use a template for this, so I came to the conclusion I would have to use another template library like handlebars. I really do not like this approach, is there another 'best practices' way for solving this problem?
I recommend using module node-env-file. That's what I use for my projects and I'm pretty happy with it. It's very simple to setup (check out their docs) and works like a charm. It reads your variables from an .env file and embeds them into your Node project.
.env
# your environment variables
API_KEY1=abcd123
app.js
var env = require('node-env-file');
var API_KEY1 = process.env.API_KEY1;
I do the following in one of my projects - it works for me with my nodemon development environment. I store the same data via config vars in Heroku so as not to have to include the keys in my repository.
// loads confidential parameters to inject into process.env
var googlekey = fs.readFileSync("./ignored/privatekey.pem", "utf8");
var config = require("./server/config/local.env");
config.PRIVATE_KEY = googlekey;
//run server using nodemon
gulp.task('serve', function(){
return nodemon({
script: 'index.js',
watch: 'server/',
// ignore: ['app/**/*', 'dist/*', 'node_modules/*'],
env: config
})
.on('start', function () {
// done();
});
});
I'm going to assume you're using what Google refers to as a 'Browser key'. If, however, you're using a 'Server key', then you shouldn't be passing the key to your client-side code anyway.
If you're using Express response.render() to generate the HTML, you can pass the API key into res.render as a local, and reference it within your HTML or template.
The following is a snippet from a jade template I'm rendering with Express, that does something similar with the Stripe API:
extends layout
block head-content
script(src='https://js.stripe.com/v2/')
script.
// <![CDATA[
Stripe.setPublishableKey('#{stripePublicKey}');
// ]]>
It sounds like you're storing the key in an environment variable, so you could pass it into the Express rendering engine thus:
res.render('index', {stripePublicKey: process.env.STRIPE_PUBLIC_KEY});
It also sounds like you want to avoid using jade or some other template engine, so you can use EJS, which will allow you to use 'pure' HTML, but with variables. Here's a good tutorial for getting started:
http://robdodson.me/how-to-use-ejs-in-express/
If you have a large amount of content already, and you don't want to have to rename all of your existing .html files to .ejs just to get that one variable passed in, you can do this:
app.engine('html', require('ejs').renderFile);
as described in more detail here:
http://expressjs.com/api.html#app.engine
It sounds like you're already working on the other best practices list under 'Best practices for securely using API keys' at the link below, but be sure especially you're following the guidance regarding restricting access by referrer URL.
https://developers.google.com/console/help/new/
Best of luck!
Tim

How do I make AngularJS work in Google Apps Script?

I am currently following this tutorial:
https://scotch.io/tutorials/build-a-real-time-scheduling-app-using-angularjs-and-firebase#connecting-to-and-using-firebase, but it is not working as I have it in Google Apps Script.
I am trying to use AngularJS in Apps Scripts. However, the documented fixes to make AngularJS work is documented to use the following line of code:
var ui = HtmlService.createHtmlOutputFromFile('myPage')
.setTitle('My Title');
ui.setSandboxMode(HtmlService.SandboxMode.IFRAME);
Source: Angular JS in Google Apps Script
But I am not sure where to put this in my Code.gs file? I have a function.doGet, so does it go in there?
Right now, my Code.gs is as follows:
function doGet() {
return HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
Thanks for your help!
HtmlService.createOutputFromFile(...) returns an instance of the HtmlOutput class, which has the setSandboxMode(..) method. Assuming that you have a file "index.html" in your Apps Script project, your code is correct:
function doGet() {
return HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
doGet is the method called by the Apps Script runtime when user navigates to your app's URL, and it should return the fully formed HTML that you want to render (which can include references to externally hosted js, css, etc.)

Angularjs & Google Analytics Integration

I'm using AngularJS UI Router, and revolunet/angular-google-analytics
https://github.com/revolunet/angular-google-analytics
Here it is my config:
.config(function(AnalyticsProvider) {
// initial configuration
AnalyticsProvider.setAccount('UA-XXXXXXX-X');
// track all routes/states (or not)
AnalyticsProvider.trackPages(true);
// Use analytics.js instead of ga.js
AnalyticsProvider.useAnalytics(true);
// change page event name
AnalyticsProvider.setPageEvent('$stateChangeSuccess');
}))
Now is there any other steps to complete? I have NOT modified any states/controllers to include any analytics code, is there anything else required? And what does the following comment means? I guess enabling 'trackPages' is enough, am I right?
.run(function(Analytics) {
// In case you are relying on automatic page tracking, you need to inject Analytics
// at least once in your application (for example in the main run() block)
})
Official reply from #revolunet
https://github.com/revolunet/angular-google-analytics/issues/35
You can check if it works in the chrome network console. check calls
to google. (via an image i guess)
About the Analytics you need to inject it manually at least once
somewhere in your app. (the run block is the good place) so it can be
instantiated by angular.

salesforce - how to make API_Session_ID available to javascript

I have a javascript app running in a home page component. It calls into a web service, which in turn calls the salesforce API. I would like to use the API_Session_ID variable to save re-logging in. How do I expose this to the javascript running in my home page component? Ideally I would like to use apex to render a small piece of script, initialising a javascript variable with the API_Session_ID variable, onto all pages, but I can't see how to do this.
Note - I can't use VF as the app is running in a home page component. The normal solution for that (run as VF in an iframe) is not available to me as I need to access the javascript objects in the main page, which I can't do from an iframe.
Ok so I figured this out. There's a value 'sid' in the session cookie, and that's what one needs with a bit of tweaking. Easy when you know how! Here's the link:
http://forums.sforce.com/t5/forums/forumtopicprintpage/board-id/general_development/message-id/7209/print-single-message/false/page/1
It's a bit out of date. I pass the cookie sid value, and window.location.host to my php web service. The php to make these values work is:
$sidparts = explode("!", $feed->sid['value']);
$location = "https://".parse_url($feed->server['value'],
PHP_URL_HOST)."/services/Soap/u/20.0/".$sidparts[0];
$sessionId = "{$feed->sid['value']}";
I've tried to do the same thing a few times as well. Unfortunately, I haven't had success without VisualForce.
I did find a discussion thread on the topic a few days ago, though.
http://boards.developerforce.com/t5/AJAX-Toolkit-S-Controls/Detect-current-user-with-AJAX-toolkit/m-p/277241#M8485
Also, it sounds like you know this, but to get the Session ID using VisualForce:
<script type="text/javascript">
var __sfdcSessionId = '{!GETSESSIONID()}';
</script>

Resources