Google Map in PhoneGap/Cordova application

I wanted to figure out how to use Google Maps API in the Phone GAP application so i built this sample application that lets you enter address and it displays that address on the Map, you can download the sample application from here This is a screen shot of how my application looks like
This is how the index.html page for my application looks like

<!DOCTYPE html>
    <title>Google Map Example</title>
 <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
    <script type="text/javascript" charset="utf-8" src="jquery.js"></script>
    <script src=""></script>
    <script type="text/javascript" charset="utf-8">
    function displayCurrentLocation(){
     console.log("Entering displayCurrentLocation()");
   var currentLocationLatAndLong = new google.maps.LatLng(37.422006,-122.084095);
   var mapOptions ={
    mapTypeId: google.maps.MapTypeId.ROADMAP
   var mapDiv = document.getElementById("map");
   map = new google.maps.Map(mapDiv,mapOptions);
   console.log("Error occured in ConsultantLocator.displayMap() " + e);
     console.log("Exiting displayCurrentLocation()");
    function addMarker(latLng,title,contentString){
  console.log("Entering addMarker()");
  var markerOptions = new google.maps.Marker({
    map: map,
    position: latLng,
  var marker = new google.maps.Marker(markerOptions);
  var infoWindowOptions = {
   content: contentString,
   position: latLng
  var infoWindow = new google.maps.InfoWindow(infoWindowOptions);
  google.maps.event.addListener(marker, "click", function(){;
  console.log("Exiting addMarker()");
    function getLatLangFromAddress(address){
     console.log("Entering getLatLangFromAddress()");
     var geocoder = new google.maps.Geocoder();
     geocoder.geocode( { 'address': address}, function(results, status) {

      if (status == google.maps.GeocoderStatus.OK) {
    var returnedValue =results[0].geometry.location;
       console.log("Address found is " + returnedValue);
       alert("Geocode was not successful for the following reason: " + status);
     console.log("Exiting getLatLangFromAddress()");
    function addMarkerForAddress(){

     console.log("Entering addMarkerForAddress()");
     var address = $("#address").val();
     var latLangForLocation = getLatLangFromAddress(address);
     console.log("Value returned by getLatLangFromAddress " +latLangForLocation);
     console.log("Exiting addMarkerForAddress()");
 document.addEventListener("deviceready", displayCurrentLocation, false);
  <body >
    <input type="text" name="address" id="address" />
    <input type="button" id="getLocation" onclick="addMarkerForAddress()" value="Get Location"/>
    <div id="map" style="width:100%; height:100%"></div>
This application loads the google map during startup. When user enters address and clicks on get location it takes the address and uses it to find the latitude and longitude for that address and then users addMarker() method to display maker for that location


Amit said...

nice example but....

When I use Jquery-mobile use with this example map will not show in my example

Anonymous said...

If you're running it on IOS you will need to open up the white list of external hosts to support *

rajiv said...

sir, thanks for this code, but when i am using this code in my project. i am not getting user enter position, only saved latitude and longitude position is showing always. i9 want to show map for user enetered position.