谷歌地图api v3显示了半个地图?

时间:2013-07-11 21:53:42

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

我正在Jquery Mobile做一个项目并且遇到了Google API的问题。当我从不同页面导航到MapPage时,MapPage本身会显示地图确定,但是当从HomePage移动到MapPage时,它只显示地图的一半。如果我手动刷新或调整地图大小,它将修复它 - 但我已经尝试了map.resize()没有运气,所以我需要一个不同的解决方案。

enter image description here

我在下面附上了HomePage和MapPage的HTML代码:

<!DOCTYPE html>
<html>

  <head>
     <meta charset="utf-8" />
     <meta name="description" content="Project - Mobile" />
     <meta name="author" content="Yuval Vardi" />
     <meta name="viewport" content="width=device-width, initial-scale=1">

     <title>RentAcar</title>

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
    <script src="JS/jquery-2.0.2.js"></script>
    <script src="JS/jquery.mobile-1.3.1.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script src="JS/mapScript.js" > </script>
    </head>

 <body>
<div id="HomePage" data-role="page" data-add-back-btn="true">
     <div data-role="header">

 </div> 
   <div data-role="content">  
     <a href="#calculate" data-role="button"  data-theme="b" data-icon="gear" data-iconpos="top">Trip Info</a>
  </div>
 <div data-role="footer" class="mainFooter"> 
   <h2> &copy; Copyright Yuval Vardi  </h2>  
   <p class="yearNow"> </p> 
  </div> 

  </div> 



 <div id="calculate" data-role="page" data-add-back-btn="true"> 

       <div data-role="header">

       </div>

       <div data-role="content">

             <table id="mapTable">
                 <tr>
                    <td>
                        <p>Pickup location</p> 
                    </td>
                    <td></td>
                    <td>
                        <p>Where do you go?</p> 
                    </td>
                 </tr>
                 <tr>
                     <td>
                          <input id="startInput" type="text" value="" placeholder="Start location" />
                     </td>
                     <td></td>
                     <td>
                         <input type="text" id="end" placeholder="Return location"/>
                     </td>
                 </tr>
                 <tr>
                     <td data-role="controlgroup">
                         <button id="myLocation" onclick="getMarker()" data-role="button" data-theme="e" > Find me </button> 
                     </td>
                     <td></td>
                     <td data-role="controlgroup">
                          <button onclick="calcRoute()" data-theme="b" > Calculate rout   </button>
                     </td>
                 </tr>
                 <tr>
                    <td colspan="3">

                    <div id="map-canvas" style="width:400px; height:280px;"> 
                    </div> 

                    </td>
                 </tr>

             </table>

       </div>

       <div data-role="footer" class="mainFooter"> 
          <h2> &copy; Copyright Yuval Vardi </h2>  
          <p class="yearNow"> </p> 
          <a class="ui-btn-right" href="#HomePage" data-role="button" data-icon="home" data-iconpos="top" >Home</a>

       </div>   

  </div>

   </body></html>

我的javascript文件:

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;


function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();   
    var israel = new google.maps.LatLng(32.0938254, 34.7786934); // the var for our initial point
    var mapOptions = {
        zoom: 7,    
        mapTypeId: google.maps.MapTypeId.ROADMAP, 
        center: israel // where to center the map on initial!        
    };

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

function calcRoute() {
    initialize();
    var start = document.getElementById('startInput').value;
    var end = document.getElementById('end').value;
    var distanceInput = document.getElementById("distance");

    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
        }
    });}

    google.maps.event.addDomListener(window, 'load', initialize);


    function getMarker()  {           
        if (navigator.geolocation)  {
            navigator.geolocation.getCurrentPosition(showPosition,showError);
        }
    }

    function showPosition(position) {
        var lat=position.coords.latitude;
        var long=position.coords.longitude;                          
        marker(lat,long); //calls the marker function
    }

    function showError(error)
    {
        alert ("Error loading map");
    }

    function marker(lat,long) {
        var myLatlng = new google.maps.LatLng(lat,long);
        var mapOptions = {
            zoom: 12,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

    $("#startInput").attr("value",myLatlng);       
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title:"You are Here!"
    });
}

2 个答案:

答案 0 :(得分:0)

地图div的宽度为零。要使用百分比宽度,您必须定义其所有父项的宽度(或至少可以用于计算宽度的固定大小元素)。

请参阅Mike Williams' Google Maps API v2 tutorial page on percentage sizing

答案 1 :(得分:-1)

您需要调用checkResize()API函数。像这样:

map = new google.maps.Map(document.getElementById("map"), mapOptions);
map.checkResize()

这个也适合我