2012-01-17 3 views
4

場所の位置を示すマーカー付きのGoogleマップを読み込むはずの位置情報アプリ(JQuery Mobileを使用した電話帳)があります。最初のロード後、マップは更新されません。私は住所に基づいて異なる場所を表示したい。JQuery Mobile:Googleマップを更新できません

私が死んでもライブはアンバインドされていないようです。

ここにコードがあります。

function loadMap(indexClicked){ 
    var offerPosition = getLatAndLong(indexClicked); 
    var centerSettings = { 'center': offerPosition, 'zoom': 10 }; 
    initMap(centerSettings,indexClicked); 
    refreshMap(); 
} 

function initMap(centerSettings,indexClicked){ 
    $('#load-map').live('pageshow', function() { 
    $('#map-canvas').gmap({'center': centerSettings.center, 'zoom': centerSettings.zoom, 'disableDefaultUI':true, 'callback': function() { 
     var self = this; 
     self.addMarker({'position': this.get('map').getCenter() }).click(function() { 
      self.openInfoWindow({ 'content': showAdrressInMap(indexClicked) }, this); 
     }); 
    }}); 
    }); 
} 

function refreshMap(){ 
    $('#load-map').live('pageshow', function() { 
    $('#map-canvas').gmap('refresh'); 
    }); 
} 

クリックされたボタンごとにloadMap関数が呼び出されます。

PS:最初の読み込み後、マップはキャッシュされたように見え、毎回同じアドレスを返します。マーカーをクリックすると、ツールチップのアドレスが更新されますが、位置は同じように見えます。 jquery.ui.map.js、jquery.ui.map.services.js、jquery.ui.map.extensions.js、modernizr.min.jsと一緒にjquery mobile 1.0 with phone gap 1.3.0を使用しています

+0

同じ問題があります 'マーカーをクリックすると、ツールチップのアドレスが更新されますが、位置は同じように見えます。'答えを提供してください... –

答えて

0

マーカーをクリアしたり、新しいマーカーを追加したり、jQuery-ui-mapの中心位置を動的に設定するには、次の方法を使用することができます。

//clear all markers 
$('#map_canvas').gmap('clear', 'markers'); 

// add a new marker 
var $marker = $('#map_canvas').gmap('addMarker', {id: i, 'position': new google.maps.LatLng(cityList[i][1], cityList[i][2]), title: cityList[i][0]}); 

// on marker click show the description 
$marker.click(function() { 
    $('#map_canvas').gmap('openInfoWindow', {'content': cityList[this.id][0]}, this); 
}); 

// update the map's center position    
$("#map_canvas").gmap("get", "map").panTo(new google.maps.LatLng(cityList[i][1], cityList[i][2])); 

Google Maps documentationをよれば、panTo方法は、所与のLatLngBoundsを含むのに必要な最小限の量だけ地図をパン。できるだけ多くの境界が見えることを除いて、マップ上のどこに境界があるかは保証されません。境界線は、地図上に存在する場合は、地図タイプとナビゲーション(パン、ズーム、ストリートビュー)コントロールで囲まれたエリアの内側に配置されます。境界が地図より大きい場合、境界の北西の角を含むように地図がシフトされます。マップの位置の変化がマップの幅と高さの両方よりも小さい場合、トランジションはスムーズにアニメートされます。

以下にサンプルを見つけることができます。あなたはシカゴボタンを押すと、私はこれが役に立てば幸いマーカーが追加され、マップの中心位置は、シカゴ

<!doctype html> 
<html lang="en"> 
    <head> 
     <title>jQuery mobile with Google maps - Google maps jQuery plugin</title> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
     <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
     <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 
     <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"> </script> 
     <script type="text/javascript" src="http://jquery-ui-map.googlecode.com/svn/trunk/ui/min/jquery.ui.map.min.js"></script> 
     <script type="text/javascript"> 

      var mobileDemo = { 'center': '41,-87', 'zoom': 7 }, 
       cityList = [ 
        ['Chicago', 41.850033,-87.6500523, 1], 
        ['Kentucki', 37.735377,-86.572266, 2] 
       ]; 

      function initialize() 
      { 
       $('#map_canvas').gmap({ 'center': mobileDemo.center, 'zoom': mobileDemo.zoom, 'disableDefaultUI':false }); 
      } 

      function addMarker(i) 
      { 
       //clear all markers 
       $('#map_canvas').gmap('clear', 'markers'); 

       // add a new marker 
       var $marker = $('#map_canvas').gmap('addMarker', {id: i, 'position': new google.maps.LatLng(cityList[i][1], cityList[i][2]), title: cityList[i][0]}); 

       // on marker click show the description 
       $marker.click(function() { 
        $('#map_canvas').gmap('openInfoWindow', {'content': cityList[this.id][0]}, this); 
       }); 

       // set the map's center position 
       $("#map_canvas").gmap("get", "map").panTo(new google.maps.LatLng(cityList[i][1], cityList[i][2])); 
      } 

      $(document).on("pageinit", "#basic-map", function() { 
       initialize(); 
      }); 

      $(document).on('click', '.add-marker', function(e) { 
       e.preventDefault(); 
       addMarker(this.id); 
      }); 
     </script> 
    </head> 
    <body> 
     <div id="basic-map" data-role="page"> 
      <div data-role="header"> 
       <h1><a data-ajax="false" href="/">jQuery mobile with Google maps v3</a> examples</h1> 
       <a data-rel="back">Back</a> 
      </div> 
      <div data-role="content"> 
       <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;"> 
        <div id="map_canvas" style="height:350px;"></div> 
       </div> 
       <a href="#" id="0" class="add-marker" data-role="button" data-theme="b">Chicago</a> 
       <a href="#" id="1" class="add-marker" data-role="button" data-theme="b">Kentucki</a> 
      </div> 
     </div>  
    </body> 
</html> 

に更新されます。

+0

$( '#map_canvas').gmap( 'destroy')を使用して新しいマーカー位置で再作成しなければなりませんでした。古いマーカーをクリアしてセットアップがうまくいかなかった.... –

関連する問題