【问题标题】:Geolocation on Android with google maps v3 & jQuery Mobile使用 google maps v3 和 jQuery Mobile 在 Android 上进行地理定位
【发布时间】:2012-05-12 20:10:55
【问题描述】:

我按照本教程http://www.mobiledevelopersolutions.com/home/start/twominutetutorials/tmt4part1 进行操作,但遇到了一个问题。地理位置在默认的 Android 浏览器上不起作用。它适用于 Chrome、IE 和 Android 版 Chrome。但不是默认的 Android 浏览器。

我想我必须把 { 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");
});

感谢您的帮助!

【问题讨论】:

    标签: android jquery-mobile google-maps-api-3 geolocation


    【解决方案1】:

    这可能是您需要调用的方式。

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

    当然,您可以在此处更改 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 } ); 
    

    【讨论】:

    • 我试图把它放在不同的地方,但我无法让它工作。你能指出我必须在我的代码中放置它的确切位置吗?谢谢!
    • 当我把它放在那个地方它不起作用,但它也停止在 IE 和 Chrome 中工作。
    • :D {maximumAge:Infinity, timeout:0, enableHighAccuracy: true } 不起作用 {enableHighAccuracy: true } );作品!感谢您的帮助。
    • 将您的答案中的 {maximumAge:Infinity, timeout:0, enableHighAccuracy: true } 更改为 {enableHighAccuracy: true },我将标记为答案!
    【解决方案2】:

    由于您想使用地理定位,您是否将传感器设置为 true?因为如果你把它设置为false,它就不起作用了。

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

    【讨论】:

    猜你喜欢
    • 2013-05-01
    • 2012-06-13
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2012-02-09
    • 2013-04-08
    • 2012-08-18
    • 2015-12-11
    相关资源
    最近更新 更多