I'm having trouble determining the best way to import a third party library such as the Google Maps JavaScript API into a UI5 application. I have considered the following methods so far:
Using a <script> tag in index.html, such as
<script src="https://maps.googleapis.com/maps/api/js?key=api_key"></script>
This works when testing locally but as mentioned in this SAP blog post, an app that is launched from the SAP Fiori Launchpad launches via the Component.js, not the index.html
That same blog post mentions importing a library via sap.ui.define, which is in fact what I'm doing with moment.js already in the application, which works. The problem is that the Google Maps API does not expose a global instance name / does not consist of a single JS file which can be imported, and thus this import fails. A similar question was asked in June last year but did not get answered.
A comment on that same question mentions using require.js to import the Google Maps API. This will likely work but seems like a hack. It feels like there should be some UI5-specific way to do this without having to import yet another third-party library.
Finally, this SAP blog post suggests manually creating and appending the Google Maps import <script> tag into the DOM once the window has loaded. This also seems like a hack but might be the only way to achieve this for the time being.
Any help determining the correct method of achieving this would be appreciated!
Well, I would do it like this:
Create custom control for map (better from sap.ui.core.HTML);
In init method of this control, load GoogleMaps by adding script tag to the page (no load event required here, because page is already loaded when this code executes);
In renderer of this new control, create a div where map will be rendered;
HTML Control has very important property - "preferDOM", this has to be set to true, otherwise, when UI5 rendering cycle happens, GoogleMap will be destroyed and created again which is very bad user experience in this case.
There are lots of different ways to implement google maps in UI5 apps:
If you are using SAPUI5: there are controls for generic map integration which you can configure to use google maps as provider:
https://sapui5.hana.ondemand.com/#/entity/sap.ui.vbm.GeoMap
https://sapui5.hana.ondemand.com/#/entity/sap.ui.vk.MapContainer
If you are using OpenUI5 you may be interested in this community library that wraps the google API in custom controls:
https://github.com/jasper07/openui5-googlemaps
If you are in need of something else, you can inject the JavaScript of google directly as the above poster explained and inject the map into the app but it might not be flexible enough. You can always create a simple custom control rendering an empty div for the map and connect it with the maps API to render the map via google API's.
However, pay attention to the Google TOS, they might require a license to use the maps API productively in your code.
Have Fun,
Michael
I ended up doing the following, which seems to provide the desired results:
jQuery.sap.includeScript(
"https://maps.googleapis.com/maps/api/js?key=api_key",
"includeGoogleMaps",
function () {
// Do things
}
);
I found this solution in the following Stack Overflow questions:
Using external js libraries in sapui5
UsingjQuery.sap.includeScript().then() in HCP Firori Launchpad
If there's anything wrong with the proposed solution, please let me know.
Related
Google have decided to replace their Map Style tool with the Cloud-base Maps Styling.
The new Beta tool allows you to import JSON map styling, but it provides no way of exporting a map style created with the Beta tool.
A colleague of mine has created a custom Map Style, but now we need to copy it into another project, but there appears no way to do this. We can duplicate the style, but only into the same project.
Is there any way around this, eg. a GCP console command.
I've tried inspecting the network traffic to see if I can grab the config, but it's in a completely different format that can't be exported.
Edit: It may appear I've done no research on this question, but the fact is that because it's referring to a Beta GCP tool, there is very little information available on its use other than:
A blog post
A sidebar which concentrates on the publishing side
All the Maps API docs refer to using a Map ID, which isn't the problem here.
Another Edit:I've created an feature request, in case anyone is interested...
For a bit of context, I'm building a client-side webpart (SharePoint Framework) to calculate routes given waypoints specified by the user. The project has been scaffolded using React and TypeScript.
So far, I've managed to display the map using Tom Chen's react-google-maps. I've used the withScriptjs method to asynchronously load Google Maps API V3 and it's working fine i.e. the map shows up.
Now the problem is I can't get hold of the 'google' object needed to access the Directions Service (which will be used to calculate and render routes on the map)
Here on StackOverflow, similar answers have been asked and from the answers, I get that the problem comes from the fact that the code using the object
public componentDidMount() {
console.log(new google.maps.DirectionsService());
}
gets executed before the API has fully loaded.
I've tried several things to make it work but nothing seems to work:
The API can't be loaded using script tags (I don't know if this is due to the specifics of my project) which is why I've used the built-in method withScriptjs provided by the package (react-google-maps).
I tried to add the API as an external resource (first, I installed the google maps typescript typings and then added the followings lines in the config.json file of the project:
Code Snapshot:
Didn't work.
I also tried the react-async-script-loader to load the API but it didn't work either.
My latest attempt has been to use Jam3/google-maps-api to get the google.maps Object using a promise once it has been resolved. It failed too.
Maybe,I missed something while trying out these different solutions. As of now, I'm going through each of them once more to be certain I've fully explored any solution.
I'd appreciate some help, thanks!
I have to include the google map API in j2me application. I want to change the route and change the center position dynamically. I have searched a lot. Now I am using google static map API but it shows the image statically. I want to show the map dynamically.
Using javascript API v3 shall I implement google map API in j2me. But one of forums I have found we can't use javascript API in j2me application. Please Give clear idea about this why we can't implement in j2me.
I haven't taken a deeper look, but I found these two links when searching Google.
http://www.developer.nokia.com/Community/Wiki/Archived:Google_Static_Maps_API_in_Java_ME
http://www.jappit.com/blog/midmaps-google-maps-java-me-library/
Whereas it is technically possible to use a static mapping API to display an map in Java ME, If you want to dynamically update the map, you would be much better off using a library which uses a tile server, caches your map tiles and overlays objects on top of it.
This should result in a much lower data traffic overhead as explained here. Only use a Map Image API if all you need is a single map image.
The dynamic mapping library I would suggest is Nokia's HERE Maps API for Java ME, as you can tell from the name, the API is specifically designed to work with Java ME devices.
The API is currently bundled with the Nokia Asha SDK 1.0, but despite this, it is in reality a separate independent plugin and has been designed to work with the full range of standard Java ME devices.
A similar Stack Overflow question answered here describes how to download it.
As a notice of affiliation, I should mention in passing that I do work for Nokia.
I have seen the two forms of reference to the Google Places Library/Service, using JavaScript vs calling the API directly, a number of times, but I don't understand the difference. The Google Docs don't describe anything about two methods of accessing the API.
For example, this question talks about 2 ways of accessing the API: OVER_QUERY_LIMIT in a loop
And it appears that there is some type of direct web access taking place in this question: Querying Google Places API using jQuery
Is this something where there was an old way that involved formatting URL parameters and the new way is by utilizing the JavaScript library calls?
FINAL SUMMARY EDIT: There are two distinct ways of requesting data from Google, as described in #Dan Nissenbaum's answer below. And since my original question, the QUERY_LIMIT question referenced above has been edited to also include more information about the two options.
Perhaps you are referring to the distinction between the Google Places API that is intended for use on the SERVER (i.e., utilizing PHP to call the Google Places API directly), and using the completely different approach of the Google Places Javascript Library in which the BROWSER executes Javascript using the Javascript library provided by Google (that internally wraps calls to the Google Places API, so that you, as a Javascript programmer, only need to understand the Javascript library provided by Google, and use that)?
Here are the two scenarios.
Scenario #1: Use the API directly. For this method, you must refer to Google's API documentation for the Google Places API: https://developers.google.com/maps/documentation/places/.
Using this API works as follows (giving a simple example only). Say you want to retrieve places within 1000 meters of latitude=-27.2531166, longitude=138.8655664. You need to hit a URL as described by the API documentation: https://developers.google.com/maps/documentation/places/#PlaceSearchRequests.
In this example, the URL looks like this (it's long):
https://maps.googleapis.com/maps/api/place/search/json?location=-27.2531166,138.8655664&radius=1000&sensor=false&key=AddYourOwnKeyHere
You need a key for your personal use, which I assume you have. There are other options you can specify, such as limiting the results to restaurants, etc.
When you hit this URL, the data will be returned in either JSON, or XML format, as specified by the text json in the URL above (use the text xml for xml). This data is returned exactly like data is returned from any URL call when you hit a URL in your browser.
You can test this by simply typing the URL directly in your browser, and see the results.
To use the API directly from code, you will need to use code that hits the external URL above within code and retrieves the results within code (for example, using the PHP CURL library, or using AJAX in Javascript).
Scenario #2: You use the Javascript library that Google provides that wraps the API, so you don't need to deal with it. I'll update the answer with more details about this, if you don't know what this is.
The docs do discuss the two different approaches. The Places Library utilizes the Google Places services from within the JavaScript Google Maps API. If you are using the Google Maps API in a browser, this is probably the approach for you:
https://developers.google.com/maps/documentation/javascript/places
There is also a web service, which allows you to query directly from your application. You query it using direct http calls to Google services. If you need access to the data on your server or a mobile device, this is the approach you want to take:
https://developers.google.com/maps/documentation/places
Despite loading the Google Maps API, via this url:
http://maps.gstatic.com/intl/en_ALL/mapfiles/276b/maps2.api/main.js
I'm getting the above error.
Why is it that when I download that URL in my browser and do a find for 'GOverlay' I'm getting zero matches?
Have Google removed this from their API or something, causing all my code to break?
GOverlay is an essential part of the Google Maps API implementation, see the V2 documentation for GOverlay here.
The reason for you not finding "GOverlay" when searching through the Javascript file you provided is simply that the Google Maps API consists of several Javascript files, not all of the code is in main.js. Additionally the code is obfuscated which could mean the build GOverlay by concatenating some crazy strings.
On a basic note: Why do you want to use some static JS file? The offical way to use the Google Maps API is using a key, which you have to obtain by registering with your Google account. So actually the URL you should be using is:
http://maps.google.com/maps?file=api&v=2&key=abcdefg
Or use the AJAX loader as seen on the Google Maps V2 documentation here.
The code is actually loaded asynchronously.
Post a link to your page - it is more than likely that there's some issue with your code.