如何使谷歌地图像Android手机中的谷歌地图应用程序一样工作

时间:2014-05-15 05:23:25

标签: javascript android jquery google-maps jquery-mobile

正如您在图片中看到的,我在屏幕左下角有放大缩小按钮。我想让它移动一点点或者可能添加一些新的CSS以便它更容易使用。有人可以帮我解决这个问题吗?

enter image description here

谢谢&问候

.JS

<script type="text/javascript">          
/*
 * Google Maps documentation: http://code.google.com/apis/maps/documentation/javascript/basics.html
 * Geolocation documentation: http://dev.w3.org/geo/api/spec-source.html
*/
$( document ).on( "pageshow", "#map-page", function() {
    <?php $coordenadas=explode(',',$fila['Googlemap']);?>

   var defaultLatLng = new google.maps.LatLng('<?php echo $coordenadas[0];?>','<?php echo $coordenadas[1];?>');
    drawMap(defaultLatLng); // Default to Hollywood, CA when no geolocation support
     //var latlng = marker.getPosition();
    function drawMap(latlng) {
        var myOptions = {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
        // Add an overlay to the map of current lat/lng
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: "Greetings!"
        });
        $( window ).resize(function() {
       google.maps.event.trigger(map, 'resize');
                      });

        map.setCenter(defaultLatLng);
    }

}); 
</script>

HTML

<div  data-shadow="false" data-theme="c"  id="map-page"  data-role="page">
<div data-role="header" style="background:#006699 !important;color:#fff;">
<a data-rel="back" href="#pageone"  class="ui-nodisc-icon ui-new-btn" data-icon="location" data-iconpos="notext"  data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="d" title="Close">Go to Page One</a>
<h1><?php echo $translate->text('Ubicación Aproximada')?> </h1>
<a data-rel="back"  href="#pageone"  class="ui-nodisc-icon ui-new-btn" data-icon="delete" data-iconpos="notext"  data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="d" title="Close">Back</a> 
</div>

    <div role="main" class="ui-content" id="map-canvas">
        <!-- map loads here... -->
    <!---</div> ---->
</div>
</div>


</div>

EDIT1

将以下脚本添加到

$(window).bind( 'orientationchange', function(e){
    var ori = window.orientation ;
        w = (ori==90 || ori==-90) ? screen.height : screen.width;
        h = (ori==90 || ori==-90) ? screen.width : screen.height;
         $('#map-canvas').width(w);
         $('#map-canvas').height($(window).height()- $('#head1').height());


    });

结果为enter image description here

答案

以下是我的问题的答案,它就像移动设备中的谷歌地图应用程序一样

<script type="text/javascript">          
/*
 * Google Maps documentation: http://code.google.com/apis/maps/documentation/javascript/basics.html
 * Geolocation documentation: http://dev.w3.org/geo/api/spec-source.html
*/


$( document ).on( "pageshow", "#map-page", function() {
    <?php $coordenadas=explode(',',$fila['Googlemap']);?>

   var defaultLatLng = new google.maps.LatLng('<?php echo $coordenadas[0];?>','<?php echo $coordenadas[1];?>'); 

   $('#map-canvas').height( $(window).height() - $('#head1').height());
    drawMap(defaultLatLng); // Default to Hollywood, CA when no geolocation support
     //var latlng = marker.getPosition();
    function drawMap(latlng) {
        var myOptions = {
            zoom: 10,
            center: latlng,
            streetViewControl:true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

        // Add an overlay to the map of current lat/lng
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: "Greetings!"
        });
        // this is our gem
            google.maps.event.addDomListener(window, "resize", function() {
            var center = map.getCenter();
            google.maps.event.trigger(map, "resize");
            map.setCenter(center); 
        }); 
}       
}); 
$(window).bind( 'orientationchange', function(e){
          var ori = window.orientation ;
            w = (ori==90 || ori==-90) ? window.height : window.width;
            $('#map-canvas').width(w);
            $('#map-canvas').height( $(window).height() - $('#head1').height());    

    }); 
</script>

enter image description here

2 个答案:

答案 0 :(得分:3)

之所以发生这种情况是因为我的页面有一个标题,而id#map-canvas的div大小等于窗口大小。如果没有给出,那么我猜是默认的。

所以我提取了标题的大小,并用窗口的大小减去它,并将其设置为map-canvas。

喜欢这个

 var  v = $( window ).height();
        h= $('#head1').height();
        v = v - h;
   $('#map-canvas').height(v);

$('#map-canvas').height( $(window).height() - $('#head1').height());

所以我的完整代码看起来像这样

<script type="text/javascript">          
/*
 * Google Maps documentation: http://code.google.com/apis/maps/documentation/javascript/basics.html
 * Geolocation documentation: http://dev.w3.org/geo/api/spec-source.html
*/
$( document ).on( "pageshow", "#map-page", function() {
    <?php $coordenadas=explode(',',$fila['Googlemap']);?>

   var defaultLatLng = new google.maps.LatLng('<?php echo $coordenadas[0];?>','<?php echo $coordenadas[1];?>'); 
   var  v = $( window ).height();
        h= $('#head1').height();
        v = v - h;
   $('#map-canvas').height(v);
    drawMap(defaultLatLng); // Default to Hollywood, CA when no geolocation support
     //var latlng = marker.getPosition();
    function drawMap(latlng) {
        var myOptions = {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

        // Add an overlay to the map of current lat/lng
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: "Greetings!"
        });
        $( window ).resize(function() {
       google.maps.event.trigger(map, 'resize');
                      });

        map.setCenter(defaultLatLng);
    }

}); 
</script>

答案 1 :(得分:1)

最后我可以完全解决问题

这是脚本

<script type="text/javascript">          
/*
 * Google Maps documentation: http://code.google.com/apis/maps/documentation/javascript/basics.html
 * Geolocation documentation: http://dev.w3.org/geo/api/spec-source.html
*/


$( document ).on( "pageshow", "#map-page", function() {
    <?php $coordenadas=explode(',',$fila['Googlemap']);?>

   var defaultLatLng = new google.maps.LatLng('<?php echo $coordenadas[0];?>','<?php echo $coordenadas[1];?>'); 

   $('#map-canvas').height( $(window).height() - $('#head1').height());
    drawMap(defaultLatLng); // Default to Hollywood, CA when no geolocation support
     //var latlng = marker.getPosition();
    function drawMap(latlng) {
        var myOptions = {
            zoom: 10,
            center: latlng,
            streetViewControl:true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

        // Add an overlay to the map of current lat/lng
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: "Greetings!"
        });
        // this is our gem
            google.maps.event.addDomListener(window, "resize", function() {
            var center = map.getCenter();
            google.maps.event.trigger(map, "resize");
            map.setCenter(center); 
        }); 
}       
}); 
$(window).bind( 'orientationchange', function(e){
          var ori = window.orientation ;
            w = (ori==90 || ori==-90) ? window.height : window.width;
            $('#map-canvas').width(w);
            $('#map-canvas').height( $(window).height() - $('#head1').height());    

    }); 
</script>

现在它就像Android设备中的Google Map应用程序一样中心