How to show Google Map in sidebar example without re-setting the map when navigating to a different view

I am new to Angular/Ionic, so please bear with me, if the solution is too simple :wink:

I played with the sidebar example and things work pretty fine. There’s one thing I couldn’t find a hint how to implement:

  • I put a google map on one view (and modify it’s center location)
  • I then navigate to another view and back
  • when navigating back to my map, the map is reset to it’s default position. Also it sends an (unnecessary) request.

How can I avoid re-loading of the map and keep my map at the center location when navigating to another view and back…

I’m not sure which Google Maps implementation you’re using, but the most likely case is that your $scope is not being stored between executions (you’re probably setting your map options into a local scope which gets blown away each time).

For anything you want to keep beyond the current view lifetime, put it in a parent scope (using accessor methods), and pick it up the next time you render the view.

More of an angular question than an ionic specific question.

thx - it’s not related to Google Maps, but everything I want to keep ‘as is’ in the DOM (e.g. a small list the user re-arranged). In ‘normal’ SPA there’s a structure similar to this:

<div id="view1" class="active">
</div>
<div id="view2">
</div>
<div id="view3">
</div>


The “active” view is shown to the user. When switching to the next view (setting it’s class to “active”), all other views stays intact. From the sidebar I just switch classes.

I am just learning AngularJS and Ionic, so my question was more: HOW does the above translate to AngularJS/Ionic? There are plenty of examples out there showing how to init things, write directives, etc… but nothing seems to show how to translate commonly handled situations using AngularJS/Ionic, which is - IMO - quite interesting for anyone trying to move towards AngularJS/Ionic.

So any hints appreciated.
BTW: I solved the original problem by writing a small factory which keeps my data. While this works fine, I currently don’t see the advantage. Before I just didn’t care and when the user came back to the map, it was still where he/she left.

1 Like