Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 2

Using Google Maps API with AJAX to create a location finder app

PAIDLevel: Intermediate4:18 mins
Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 2
You do not have access to this lesson!

Enroll in the training to get access to this lesson along with hundreds of other lessons and complete courses.

Already enrolled? Login

Course content
Lessons #1: Getting Started with jQuery PAID
10:21 mins
Lessons #2: Find Something / Query the DOM with jQuery - part 1 PAID
12:52 mins
Lessons #3: Find Something / Query the DOM with jQuery - part 2 PAID
22:25 mins
Lessons #4: Find Something / Query the DOM with jQuery - part 3 PAID
14:24 mins
Lessons #5: Do Something with jQuery PAID
11:39 mins
Lessons #6: Events with jQuery PAID
19:47 mins
Lessons #7: Project: Create a Top Bar Widget with jQuery PAID
23:47 mins
Lessons #8: Project: Create Tabs functionality with jQuery PAID
10:46 mins
Lessons #9: Animate: Animating elements with jQuery PAID
9:41 mins
Lessons #10: Event Delegation with jQuery PAID
4:52 mins
Lessons #11: Event Propagation with jQuery PAID
6:20 mins
Lessons #12: Prevent Default Behavior (of elements) with jQuery PAID
3:06 mins
Lessons #13: Content Manipulation with jQuery PAID
9:32 mins
Lessons #14: Keyboard Event with jQuery PAID
7:13 mins
Lessons #15: Project: Create your own Image Carousel Slider with jQuery PAID
27:07 mins
Lessons #16: Ajax: Create a Single Page App with jQuery PAID
15:09 mins
Lessons #17: Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 1 PAID
9:53 mins
Lessons #18: Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 2 PAID
4:18 mins

Ajax: Create a Location Finder App using jQuery & Google Maps API Part 2

Continuing from our previous Create a Location Finder App using jQuery & Google Maps API part 1 lesson. We know that longitude and latitude parameters are hard coded and it's not picking up the actual location the user is at. So we are going to use the feature that HTML5 introduces called the Geo location. 

There's an object in the modern browsers called navigator, not all browsers have it, only the latest browser do. With the help of navigator you can see if the Geo location feature is available on your browser. 

I'm going to write a function and call it getLocation and I'll do navigator.geolocation to see if the Geo Location feature is available in the browser, if that condition is true, then I'm going to call a function inside that object called navigator.geolocation.getCurrentPosition and this will have a function in it which is going to run when the location is found and call the function (loc) and for demonstration purposes, I'll console.log

function getLocation(){
  if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(function(loc){
    console.log(loc);
});
}
}

The getLocation function is going to run immediately when the button (anchor tag) is clicked in the webpage. Save it, refresh the screen 


Save it, refresh the screen and now if you click on the anchor tag one more time and wait a little bit, just after couple of seconds the Geo Location will display like such.


The navigator at the top is using the HTML5 feature of Geo location from the browser. I have already enabled and allowed the browser for our web server URL to access my location. 

Well that's a privacy concern. Go to the Chrome settings if you're using chrome like I'm doing and scroll down go to the advanced settings. Go to content settings, scroll down to location and click manage exceptions and you can cancel it, if you're doing it for the first time. 

Now that I have disabled it and if I refresh the page again and click it one more time, you will see the pop up asking you to either allow or deny and if you allot it, your location is accessible but if you deny it, your location is not accessible.


And if I hit allow, it will wait a few seconds then the location information will be printed as shown below. 


Inside coords, we have latitude and longitude. Now our job is to pick up the latitude and longitude information, combine it and separate with a comma then pass it to the Google APIs.

I'm going to create a variable and call it latlng and concatenate with a comma. Also getCity function is going to be called when the location information is found. I'm going to pass latlng function to the getCity

function getLocation(){
  if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(function(loc){
    var latlng = loc.coords.latitude + ',' + loc.coords.longitude; getCity(latlng);
});
}
}

When the getCity is run, it needs the latlngInput parameter


Save it, refresh the screen and click "What City Am I In?" and you will see the city gets printed on the screen and the Geo location is working fine. Also if you want to know how it works, you can go to Network tab and click on the file and the call is being made to the Google Maps APIs and if you go to the response tab, you will see all the information regarding the city, state, county and the zip code etc.


Congratulations you just build a real application with HTML5 feature, jQuery and Google maps APIs. I hope you enjoyed this lesson, if you have any question, leave your comments below. I'll talk to you in the next lesson. Goodbye :)
Become a confident software developer with mentor support!
Get Started
Already have an account? Please Login