In this article I will tell you about how to create AngularJS Geolocation Single Page Application.
I’ll start with the fact that tell what technologies we will use.
As the name implies the fundamental technology on which to build the entire application is AngularJS.
Also, we need Geolocation JSON API
that will give us the latitude
, longitude
, city name
and other location data.
For these purposes, I decided to use JSON IP API
http://ip-api.com/json
As a card I’ll use Leaflet
. Leaflet
is an open-source JavaScript library
for mobile-friendly interactive maps. For more information about this JavaScript library, you can find the official website
http://leafletjs.com/
It is also useful to us Leaflet.awesome-markers plugin
and Wikipedia API
. But everything has its time.
So we can start
Start by creating a simple HTML page. At this stage, your index.html
should look as follows:
and main.css
should be:
Now let’s create our application’s About page.
But first we should create a starting point for application, configuring routes as well as the controller for About page.
Now let’s create js/app.js
file and configuring our routes
$routeProvider
is AngularJS directive used for configuring routes.
We also connected the router as a dependency ['ngRoute']
for work with $routeProvider
In addition, we must connect AngularJS framework for out page and angular-route module.
To do this, add this to our page within the tag <head>
now our body
tag should look like this
This will be the starting point for our application.
Now we need to create a link to a page about, template and the about controller.
In your index.html
file immediately after the line
you must to write link to page About
Now add some style for this link in your main.css
Create a file js/controllers/AboutController.js
This will our controller for About page:
We must also not forget to connect our file on the page before the closing body
tag
Now let’s create the template for our About page to be displayed when we go to /#/about
.
To do this, create a file views/about.html
and paste the following code:
And again, let’s write some styles for this page:
Since we are using ngRoute
directive, for the rendering our pages we must to use ngView
directive.
Go to the file index.html
and paste immediately after
that code
your code should look like this
Then you can go to the application About page which should look like this:
After create the template, we can begin to create the map and display some of sights points and your location point
Start by creating a factory
service in which we will receive information about the geolocation from JSON API
.
Create a file js/location.js
and write:
$q
is a service that helps you run functions asynchronously,
and use their return values (or exceptions) when they are done processing.
Next, with $http.get
request we get latitude
, longitude
, and the name of city
from JSON Geo API http://ip-api.com/json
The data returned by this JSON API
For convenient viewing of JSON in your browser, you can use JSONView Chrome Extension
Now we need create a controller for Main
page, template and configure routes in our js/app.js
.
I’ll start with the configuration routes. Open your js/app.js
file and replace it with following code:
You may have noticed that I have connected a new dependency [..., 'leaflet-directive'].
This directive allows you to embed a map on your AngularJS application and interact bi-directionally with it via the AngularJS scope and the leaflet map library API
Official website of leaflet-directive http://tombatossals.github.io/angular-leaflet-directive/
resolve:
- An optional map of dependencies which should be injected into the controller. If any of these dependencies are promises,
the router will wait for them all to be resolved or one to be rejected before the controller is instantiated.
If all the promises are resolved successfully, the values of the resolved promises are
injected and $routeChangeSuccess event is fired. [Cut from the official documentation AngularJS https://docs.angularjs.org/api/ngRoute/provider/$routeProvider]
Now, after we have finished the configuration of routes we can create MainController.
Create a js/controllers/MainController.js
file and write:
About $scope.mapCenter
you will understand when we create a templale for main page.
Create a views/main.html
file and paste:
So, in our MainController
, we have created an $scope.mapCenter
object in which the recorded data from the Geo JSON API
.
That latlilude
, longitude
and zoom
(zoom not from API).
With that object we Initial geographical center of the map and “push” it to leaflet center="mapCenter"
for visualize on map.
In this case, is your approximate location. In the future, we will put marker on your location.
Let’s add some style to css/main.css
:
Now, before you run and see application, we must connect the needed files
Connect our files on the page before the closing body tag:
and add this libraries to our page within the tags head:
Now you can run your application with command ws -p 8000
https://www.npmjs.com/package/local-web-server
and see it on your localhost:8000
Wikipedia API and Sights Points Markers
Now, after we display a map in our application and showed on it
your approximate location is the time to display some markers
of sights in your city. From Wikipedia API
.
This is no big deal.
For display coordinates of the markers is strictly on the map, we need a simple algorithm.
Create new js/helper.js
file and paste this code:
This algorithm will continue to receive data from the Wikipedia API
and display them on the map.
Just attach this file before the closing body tag
Open js/controllers/MainController.js
and paste code after $scope.mapCenter
object:
and add $http
dependency. For that replace
with
With var getSightsPoints = $http.jsonp('https ...
we get data about some sights within a radius of 10000 meters
from your approximate location. For more information about the parameters specified in the request URL you can read here https://www.mediawiki.org/wiki/Extension:GeoData
And this code
push gets from Wikipedia API data to our js/helper.js
from which our objects will be visible on the map.
But you need replace
to
in views/main.html
file. Then you can see your application in action.
Add marker for your approximate location
The last thing we need to implement our application is a marker to show your approximate location. So proceed!
For that task we will use Leaflet.awesome-markers plugin
https://github.com/lvoogdt/Leaflet.awesome-markers
Let’s just add needed files for work with this plugin to our page within the tags head
Open js/controllers/MainController.js
and replace
with
Hooray! We ended up creating this application. Now if you run this application, you should see something like this