我在Jquery Mobile上做一个项目,而Google API有问题。当我从其他页面导航到MapPage时,MapPage本身显示地图正常,但是当从HomePage移至MapPage时,它仅显示地图的一半。如果我手动刷新地图或调整其大小,它将解决该问题-但是我尝试过map.resize()时没有运气,因此我需要一个不同的解决方案。



我在下面附加了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!"
    });
}

最佳答案

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

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

关于javascript - Google Map API v3显示一半 map ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17604168/

10-11 09:21