2012-05-03 109 views
0

我跟着本教程http://www.mobiledevelopersolutions.com/home/start/twominutetutorials/tmt4part1,我有一个问题。地理位置不适用于默认的Android浏览器。它适用于Android,Chrome和IE浏览器。但不是默认的Android浏览器。Android上的地理位置与谷歌地图V3和jQuery Mobile

我想我必须把{enableHighAccuracy:true}放在某个地方,我不明白它的意思。

这是代码:

var mapdata = { destination: new google.maps.LatLng(51.3704888, 6.1723862) }; 

// Home page 
$('#page-home').live("pageinit", function() { 
    $('#map_square').gmap(
     { 'center' : mapdata.destination, 
      'zoom' : 12, 
      'mapTypeControl' : false, 
      'navigationControl' : false, 
      'streetViewControl' : false 
     }) 
     .bind('init', function(evt, map) { 
      $('#map_square').gmap('addMarker', 
       { 'position': map.getCenter(), 
        'animation' : google.maps.Animation.DROP 
});                                                    
     }); 
    $('#map_square').click(function() { 
     $.mobile.changePage($('#page-map'), {}); 
    }); 
}); 

function fadingMsg (locMsg) { 
    $("<div class='ui-overlay-shadow ui-body-e ui-corner-all fading-msg'>" + locMsg + "</div>") 
    .css({ "display": "block", "opacity": 0.9, "top": $(window).scrollTop() + 100 }) 
    .appendTo($.mobile.pageContainer) 
    .delay(2200) 
    .fadeOut(1000, function(){ 
     $(this).remove(); 
    }); 
} 

//Create the map then make 'displayDirections' request 
$('#page-map').live("pageinit", function() { 
    $('#map_canvas').gmap({'center' : mapdata.destination, 
     'mapTypeControl' : true, 
     'navigationControl' : true, 
     'navigationControlOptions' : {'position':google.maps.ControlPosition.LEFT_TOP} 
     }) 
    .bind('init', function() { 
     $('.refresh').trigger('tap');   
    }); 
}); 

$('#page-map').live("pageshow", function() { 
    $('#map_canvas').gmap('refresh'); 
}); 

// Request display of directions, requires jquery.ui.map.services.js 
var toggleval = true; // used for test case: static locations 
$('.refresh').live("tap", function() { 


      // START: Tracking location with device geolocation 
    if (navigator.geolocation) { 
     fadingMsg('Using device geolocation to get current position.'); 


     navigator.geolocation.getCurrentPosition ( 
      function(position) { 
       $('#map_canvas').gmap('displayDirections', 
       { 'origin' : new google.maps.LatLng(position.coords.latitude, position.coords.longitude), 
        'destination' : mapdata.destination, 'travelMode' : google.maps.DirectionsTravelMode.DRIVING}, 
       { 'panel' : document.getElementById('dir_panel')}, 
         function (result, status) { 
          if (status === 'OK') { 
           var center = result.routes[0].bounds.getCenter(); 
           $('#map_canvas').gmap('option', 'center', center); 
           $('#map_canvas').gmap('refresh') 
          } else { 
           alert('Unable to get route'); 
          } 
         } 
        );   
       }, 
       function(){ 
        alert('Unable to get location'); 
        $.mobile.changePage($('#page-home'), { }); 

       }); 
      } else { 
       alert('Unable to get location.'); 
      }   
    // END: Tracking location with device geolocation 
    $(this).removeClass($.mobile.activeBtnClass); 
    return false; 
}); 


// Go to map page to see instruction detail (zoom) on map page 
$('#dir_panel').live("tap", function() { 
    $.mobile.changePage($('#page-map'), {}); 
}); 

// Briefly show hint on using instruction tap/zoom 
$('#page-dir').live("pageshow", function() { 
    fadingMsg("Tap any instruction<br/>to see details on map"); 
}); 

THX的帮助!

回答

3

这是您可能需要打电话的方式。

navigator.geolocation.getCurrentPosition(successCallback, 
             errorCallback, 
             {maximumAge:Infinity, timeout:0, enableHighAccuracy: true }); 

Ofcourse在这里你可以改变maximumAge和超时值,但是这是你设置enableHighAccuracy。 所以只需在你的getcurrentposition方法中指定它作为第三个参数。

编辑:

navigator.geolocation.getCurrentPosition ( 
     function(position) { 
      $('#map_canvas').gmap('displayDirections', 
      { 'origin' : new google.maps.LatLng(position.coords.latitude, position.coords.longitude), 
       'destination' : mapdata.destination, 'travelMode' : google.maps.DirectionsTravelMode.DRIVING}, 
      { 'panel' : document.getElementById('dir_panel')}, 
        function (result, status) { 
         if (status === 'OK') { 
          var center = result.routes[0].bounds.getCenter(); 
          $('#map_canvas').gmap('option', 'center', center); 
          $('#map_canvas').gmap('refresh') 
         } else { 
          alert('Unable to get route'); 
         } 
        } 
       );   
      }, 
      function(){ 
       alert('Unable to get location'); 
       $.mobile.changePage($('#page-home'), { }); 

      }, 
    { enableHighAccuracy: true }); 
+0

我想把它放在不同的地方,但我无法得到它的工作。你能指出我的代码中的确切位置吗?谢谢! – Maxcim

+0

@Maxcim现在就试用它。 –

+0

当我把它放在那个地方它不起作用,但它也停止在IE和Chrome的工作。 – Maxcim

-1

由于要使用地理定位,你的传感器设置为true?因为如果你将它设置为false,它将不起作用。

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script> 
+0

传感器参数不会以任何方式影响结果。这只是谷歌的FYI: http://stackoverflow.com/questions/8616764/what-is-the-sensor-parameter-in-google-places-api-good-for –