yii2-google-maps-library icon indicating copy to clipboard operation
yii2-google-maps-library copied to clipboard

Using Pjax Map is not displaying

Open kodebees opened this issue 10 years ago • 18 comments

on using Pjax map is not displaying. On page load map is displaying but not pjax

        <?= \yii\bootstrap\Html::a("Refresh", ['dashboard/trip-info?trip_id='.$tripInfo->ietms_trip_id], ['class' => 'btn btn-lg btn-success', 'id' => 'refreshButton']) ?>
        <?= $map->display();?>


    <?php
    \yii\widgets\Pjax::end();
    ?>

kodebees avatar Nov 07 '15 12:11 kodebees

why would you use Pjax with Google Maps?

tonydspaniard avatar Jan 12 '16 14:01 tonydspaniard

I have the same problem. Any solutions??

aenima700 avatar Feb 04 '16 15:02 aenima700

@tonydspaniard I want to use Pjax with google maps because i have to update the marker dynamically. so i'm using pjax to get the latest lat and lng from database. Thanks Kathiresan S

rihtak avatar Feb 19 '16 06:02 rihtak

@rihtak a better solution would be to bind a js function and use ajax to update the marker dynamically if you have to. pjax is too much magic IMHO and I doubt will work smoothly with google maps js core library and its layers.

Edit: In fact, by looking at your code is a very simple method attached to a button. I would avoid the use of pjax here.

tonydspaniard avatar Mar 09 '16 00:03 tonydspaniard

Thanks for your suggestion @tonydspaniard

rihtak avatar Mar 10 '16 06:03 rihtak

@tonydspaniard Running a big project where we are using pjax, we are adding Google MAPs to some off our features. You mean we need to stop using pax then?

JfrA avatar Feb 15 '17 22:02 JfrA

@JfrA I am not suggesting not to use pJax. Maybe is good in some areas of your app, maybe in all of them. The problem with google maps is that you will need to hack its display on each reload where google maps is displayed (if its part of a Pjax'ed content, happens the same when you work with hidden layers (you need to force resize of the map to display it) or you use a modal window (it simply doesn't show, you need to reinitialize the map after modal is shown)). If you simply need to update the google maps, why bother over-complicating stuff, let it run and make your operations using ajax calls your self instead of the magic of Pjax.

If you are concerned about keeping history you can do it also your self without the need of pjax, by injecting the steps on the history and parse the url to display the correct step on the map.

tonydspaniard avatar Feb 15 '17 22:02 tonydspaniard

@tonydspaniard forcing resize does not do the trick.. Maybe explain the problem we are having.. when loading the page witch contains a Google MAP it works flawless throughout alla page loads to or from that page using pjax. however if we start with loading for example index which does not include a map and navigating to the page witch includes the map, the google api is loaded but map never shows.. you have an idea how to fix that?

JfrA avatar Feb 15 '17 22:02 JfrA

you will have to work with the events of pjax and re-initiate the map. Try that with pjax:completeevent (i would check whether the content has the map and fire up the instantiation again - ie map_init() whatever. )

tonydspaniard avatar Feb 15 '17 23:02 tonydspaniard

@tonydspaniard Yes that i know, are re-initing it for all page views.. but the the thing is if the dom and the api is on the initial page load it works fine browsing to or from with pjax: end/complete... but if the map canvas and the api is not on the initial load e.g. index and from then load in google maps it will not render. tried resizing it, tried to load through another xhr, nothing works..

JfrA avatar Feb 15 '17 23:02 JfrA

@JfrA can you share some of the code?

tonydspaniard avatar Feb 15 '17 23:02 tonydspaniard

@tonydspaniard :

$(document).on('ready pjax:end', function() {
  WhenGoogleLoadedDo(mapMyReadyFN);
}

// Just to be sure..
function WhenGoogleLoadedDo(fnt) {
    if(typeof google != 'undefined') {
        mapMyMap=null;
        fnt();
        google.maps.event.trigger(mapMyMap,'resize');
    } else {
        setTimeout(function(){(function(fnt) {
            WhenGoogleLoadedDo(fnt)
        })(fnt)}, 500);
    }
}

function mapMyReadyFN() {
    if(mapMyMap===null) mapMyInitMap();
    // Adding some infoWindows n markers....
}

function mapMyInitMap() {
    mapMyMap = new google.maps.Map(document.getElementById('mapMyMap'), {
        center: {lat: 59.918570, lng: 10.772751},
        zoom: 15,
        mapTypeId: 'hybrid'
    });

}

JfrA avatar Feb 15 '17 23:02 JfrA

There is something strange on your code. Why you checking for google only? shouldn't you check whether the element where the map should be rendered exists too? What happens if your mapMyMap doesn't exist? That could be the issue.

On another note, i would place some console.log stuff in your code. But, apart from that, the code seems fine. Only that small check.

tonydspaniard avatar Feb 15 '17 23:02 tonydspaniard

@tonydspaniard the mapMyMap exist that i know. Se if i can explain better: when refreshing browser on e.g. map.html it loads fine and i can go to index.html and back to map.html still works fine.

But when i refresh browser at index.html and then go to map.html it will not render, the canvas is there and the api is loaded but nothing displays..

JfrA avatar Feb 15 '17 23:02 JfrA

@tonydspaniard I actually got it working now. Found that pjax didn't load inline style ref.. should have found that without having to write here.. but thanks for you're effort to help!

JfrA avatar Feb 16 '17 00:02 JfrA

Glad you finally got it working. I feel I couldn't do much anyway. It always a pleasure. Good luck with your project.

tonydspaniard avatar Feb 16 '17 08:02 tonydspaniard

map1

this is view page in yii2 i want to display map(my location) in Location.thanks advance :)

shankar2993 avatar Aug 21 '17 15:08 shankar2993

to be able to help a next person I need to leave my code that I implemented

var initFollowProcessByStateMap = function () {
  $("#follow-process-by-state-map").change(function () {
    $("#country-form").submit();
  })
}

var initClickPeriodo = function () {
  $("#acompanhar-processos-por-gestao,#acompanhar-processos-por-estado").change(function () {
    $("#periodo").submit();
  })
}

function loadProcessMapsStates(fnt) {
  if (typeof google != 'undefined') {
    drawRegionsMap = null;
    fnt();
    if (google.maps)
      google.maps.event.trigger(drawRegionsMap, 'resize');
  } else {
    setTimeout(function () {
      (function (fnt) {
        loadProcessMapsStates(fnt)
      })(fnt)
    }, 500);
  }
}

function mapProcessReadyFN() {
  if (drawRegionsMap === null) mapProcessInitMap();
  // Adding some infoWindows n markers....
}

function mapProcessInitMap() {
  google.load('visualization', '1', {
    'packages': ['geochart', 'table']
  });

  var drawRegionsMap = function () {
    var data = google.visualization.arrayToDataTable(teste1);

    var view = new google.visualization.DataView(data)
    view.setColumns([0, 1])

    var options = {
      region: 'BR',
      resolution: 'provinces'
    };

    var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
    chart.draw(data, options);

    var geochart = new google.visualization.GeoChart(document.getElementById('chart_div'));
    var options = {
      region: 'BR',
      resolution: 'provinces',
      legend: {
        position: 'top',
        alignment: 'center',
        maxLines: 1
      },
      keepAspectRatio: false,
      colorAxis: {
        colors: ['blue', 'green', 'red'],
        minValue: 0,
        maxValue: 2
      } // orange to blue 
    };
    google.visualization.events.addListener(geochart, 'regionClick', function (eventData) {
      // maybe you want to change the data table here...
      options['region'] = eventData.region;
      options['resolution'] = 'provinces';
      options['displayMode'] = 'markers';

      var data = google.visualization.arrayToDataTable(teste2);

      geochart.draw(data, options);
      var table = new google.visualization.Table(document.getElementById('table'));
      table.draw(data, null);
    });
    geochart.draw(data, options);
  };

  google.setOnLoadCallback(drawRegionsMap);
}

initClickPeriodo();
initFollowProcessByStateMap();

$(document).on('ready pjax:end', function (event) {
  console.log(event.target.id);
  if (event.target.id == 'type-person') {
    initClickPeriodo();
  }

  if (event.target.id == 'country') {
    initFollowProcessByStateMap();
    loadProcessMapsStates(mapProcessReadyFN);
  }
})

$(document).ready(function () {
  $.pjax.reload({
    container: "#type-person"
  })
  $.pjax.reload({
    container: "#country"
  })
  loadProcessMapsStates(mapProcessReadyFN);
})

jhonatasfender avatar Oct 26 '18 02:10 jhonatasfender