0

I have this Javascript code on Google maps with geolocation and direction and I am using it in cordova , it gives me problems because it lacks the event Deviceready.

I tried to put it , but it was not working.

You are able to add it in the right way ?

Code:

 var directionsDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;
     // default location.  When geolocation tracks the client, this variable is set to that location

  function initialize() {
    var mapOptions = {
      zoom: 15,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };


    directionsDisplay = new google.maps.DirectionsRenderer();   
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

 //here there is marker

directionsDisplay.setMap(map);
    map.setCenter(mylocation);
  }



  function updateRoute() {
    calcRoute(mylocation);
  }


  function calcRoute(start) {
    var start = mylocation;
    var end = document.getElementById('end').value;
    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.TravelMode.WALKING,


    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  }

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      mylocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
      //document.getElementById('start').value = ;
      if (map) {
        calcRoute(position.coords.latitude +','+ position.coords.longitude);
          map.setCenter(mylocation);

          //*Posizione Utente*//
        var image = 'user.png'      
        var marker1 = new google.maps.Marker({
           position: mylocation,
           map: map,
           title: 'ciao!',
           icon: image

     }); 

    google.maps.event.addListener(marker1, 'click', function() {
    infowindow1.open(map,marker1);
    });
    var infowindow1 = new google.maps.InfoWindow({
          content: '<img src="user.png">Sono Qui.....'
       });


      }
    })
  }

    google.maps.event.addDomListener(window, 'load', initialize);
Jesuraja
  • 3,774
  • 4
  • 24
  • 48
none nane
  • 29
  • 1
  • 7
  • Where did you put the onDeviceReady listener? Which Kind of problem do you have? I think you can start the map initialization upon receiving the onDeviceReady event – nax83 Jul 13 '15 at 08:36
  • I removed everything about deviceready . There is not. I do not know how to add – none nane Jul 13 '15 at 08:51
  • take a look at this answer. It explains how to set up your code in order to receive the deviceready http://stackoverflow.com/a/10893565/1357273 basically you will init your map and do your stuff in the ondeviceready function – nax83 Jul 13 '15 at 08:55
  • it's same to cordova documentation.I have already tried to use that code , is not working . – none nane Jul 13 '15 at 09:00
  • Second answer in the linked post should answer your question to a great extent: http://stackoverflow.com/questions/12576062/jquery-document-ready-vs-phonegap-deviceready?answertab=votes#tab-top. But actually this question is still very broad as there are different ways to initialize map in Cordova since multiple platforms are integrated. – Kay_N Jul 14 '15 at 22:08
  • The snippet you provided is Google Maps API V3 whereas for Android there is Platform API (V2) as well. There are various plugins available for Cordova/Phonegap based on either of them. If you are new to it, I recommend checking them out. Hope the info helps. – Kay_N Jul 14 '15 at 22:11

0 Answers0