Google map + browser back with last view location and zoom value - google-maps

I am trying to return the last marker view and zoom level to the user when he access some other pages and returns to the map page. Right now, I am using cookies to do it. Just want to find out if there's a conventional way of dealing with this, thanks:
When the user clicks on a marker, store them into cookies:
var latLng = map.getCenter();
var currZoom = map.getZoom();
$.cookie("map_center", latLng);
$.cookie("location_id", locationid);
$.cookie("curr_zoom", currZoom);
Retrieving the last viewed marker location and zoom level when he returns to the map page:
var lastViewedActivity = $.cookie('location_id');
var lastViewedMapLocation = $.cookie('map_center');
var lastViewMapZoom = $.cookie('curr_zoom');

This is definitely the way to go; your other options would be:
storing on the server, with session or passing parameters back and forth: not appropriate here as it's more complicated and it adds nothing
html5 local storage: not supported enough, so you'd still need a fallback on cookies

Related

How to Retrieve Forge Viewer objectTree?

My goal is to highlight a room by adding new geometry to the viewer based on lines I have created in revit like they do here Link
but i can not figure out how to access those lines ids.
I know what they are in revit (element_id) but not how they are mapped as dbid.
Following this Blog Post
I want to access the objectTree in my extension to find out, but it always comes back as undefined.
var tree;
//old way - viewer is your viewer object - undefined
viewer.getObjectTree(function (objTree) {
tree = objTree;
});
//2.5 - undefined
var instanceTree = viewer.model.getData().instanceTree;
var rootId = this.rootId = instanceTree.getRootId();
//- undefined
var objectTree = viewer.getObjectTree();
Can anyone tell me if its still works for them I am using the v2 of the API for the rvt conversion to svf and 2.9 of the viewer3D.js
note I can see a list of dbid if I call this
var model = viewer.impl.model;
var data = model.getData();
var fragId2dbIdArray = data.fragments.fragId2dbId ;
but have no way of mapping back to the Revit element_id
As of version 2.9 this is still working. Here's my console:
Here's a couple of things you can try:
Is viewer undefined? Are you in the correct scope when grabbing the viewer?
The document have to be loaded before you can grab the instance tree. When the document is loaded, an event called Autodesk.Viewing.GEOMETRY_LOADED_EVENT will be fired, then you can start manipulating the instance tree.
Simply do this:
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, function () {
var instanceTree = viewer.model.getData().instanceTree;
});
For more structured code, follow this guide to add an extension.
There's a more detailed blog post on which event to listen for. It's still using the old way to get instance tree, though.
Shiya Luo was correct the viewer had not yet finished loading the geometry
in my extentions Load function I added two event listeners and made sure they both fired before trying to access the instanceTree
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, function () {
finishedGEOMETRY_LOADED_EVENT = true;
if(finishedGEOMETRY_LOADED_EVENT && finishedOBJECT_TREE_CREATED_EVENT ){
afterModelLoadEvents(viewer);
}
});
viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, function () {
finishedOBJECT_TREE_CREATED_EVENT = true;
if(finishedGEOMETRY_LOADED_EVENT && finishedOBJECT_TREE_CREATED_EVENT ){
afterModelLoadEvents(viewer);
}
});

Popup keeps showing up on iPad because it won't accept cookies

I have made a popup that is meant to load when my website loads.
I have added a cookie to remember if the user has seen the popup after closing it, so that they are not presented with the popup again. It's working well, apart from on the iPad the popup loads on every single page of the site.
Does anyone know the best way around this?
My cookie code is:
function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
$("#member").click(function(){
SetCookie("subscribed","1");
$( "#Subscribe" ).dialog("close");
return false;
});
Give HTML5 Storage a try! See: http://www.w3schools.com/html/html5_webstorage.asp
// store an item in the localstorage
localStorage.setItem('popup_shown', true);
// get an item from localstorage
var popupShown = localStorage.getItem('popup_shown');

Accessing a placemark with google earth api through Region-Based Network Linked kml files

I have a huge set of placemarks loaded using regionated kml files. (around 1000 kml files generated).
For example , I have a button, when clicked camera flies to the location of the placemark I want to access. So I think the kml file that includes this placemark is loaded after this process. Let's say this is 5.kml and I tried to get the placemark object using getElementByUrl method. But this didn't work. I can also use ge.getElementsByType("KmlPlacemark") method but I need to have a loop to get the placemark object I need. This works but I couldn't find a way to make it work fast. Below is my code
google.earth.addEventListener(ge.getView(), 'viewchangeend', function() {
// after button click and camera centered on the placemark with id 1767
var p = ge.getElementByUrl('http://localhost/Test/5.kml#1767');
alert(p.getId()); // this does not work because p is null
var placemarks = ge.getElementsByType('KmlPlacemark');
for (var i = 0; i < placemarks.getLength(); ++i) {
var placemark = placemarks.item(i);
if(placemark.getId() == 1767)
{
alert(placemark.getId()); // this works
return;
}
}
});
function button_click()
{
var camera = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);
camera.setLatitude(30);
camera.setLongitude(50);
camera.setAltitude(2000);
ge.getView().setAbstractView(camera);
}
I wish I found a way to access the object which is imported from KML(when region beomes active). Waiting for your answers. Thanks.
NetworkLink's don't load files into the DOM, which is why getElementByUrl doesn't find the Placemark you're looking for. You would need to fetch the KML. This article should be helpful in explaining the different ways to load KML in the Google Earth API.

html5-geolocation : navigator.geolocation.watchPosition continuous callbacks

I am using the HTML5 geolocation API. I recently decided to shift from polling user position to
using the watchPosition method, which is supposed to fire its "success" function, once the device position changes.
But instead, it is filling up my database with the same position over and over again. Relevant code posted below:
updateLocation = navigator.geolocation.watchPosition(success, failed, {enableHighAccuracy:true, maximumAge:30000, timeout:27000});
function success(){
showGPS(position);
}
function showGPS(position) {
var lng = position.coords.longitude;
var lat = position.coords.latitude;
gpsText.innerText = "Latitude: "+ lat +"\nLongitude: "+ lng + "\nAccuracy: "+ position.coords.accuracy + "\nSpeed:" + (position.coords.speed*3.6) + "\nAltitude:" + position.coords.altitude;
getData("http://databaseEntryURL/gpsReceiver.aspx?string=" + gpsText.innerText);
}
The getData gets called about 5 times a second. I'm sure the latlng is not changing. I'm using android 2.2, to load this page. How do i make it call the success function ONLY if the device position changes?
Maybe it's just caused by some bug, so even when the location has no change (or the change is so small) your callback is still triggered.
One way to solve it is storing the last location and using it to compare with the current location. If there's no change (or the change is too small), do nothing in the callback. This will save you bandwidth and database space.
If you need to save battery life for the phone then you need to optimize the options, for example, changing enableHighAccuracy to false.
A more efficient way would be to set the watchPosition as a variable and call clearWatch() on it once you've received the first response:
var watchID = navigator.geolocation.watchPosition(onSuccess, onFailed);
function onSuccess(position) {
navigator.geolocation.clearWatch(watchID);
};

overlay geotagged photo in Google Maps?

I want to create a Google map with user uploaded Geotagged photos that show up on my map. I can easily create manipulate my map but I can't seem to find instruction on how to add these geotagged photos.
Here is an example of what I am try to accomplish:
http://maps.google.com/?ie=UTF8&ll=26.892794,-80.055909&spn=0.003875,0.004828&t=h&z=18&lci=lmc:panoramio
I don't have experience working with photos, but I don't think it should be really any different than placing a GMarker on the map at the appropriate coordinates of your photo, and then in the info window of the tag you would output your custom HTML which would include your photo.
Edit: Specific link leading to the GMarker class in the Google Maps API Reference: http://code.google.com/apis/maps/documentation/reference.html#GMarker
You need to create a tile, then create a tile overlay.
var tilelayer = new GTileLayer(myCopyright);
tilelayer.getTileUrl = function() { return "../include/tile_crosshairs.png"; };
tilelayer.isPng = function() { return true;};
tilelayer.getOpacity = function() { return 1.0; }
var myTileLayer = new GTileLayerOverlay(tilelayer);
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
map.addOverlay(myTileLayer);
The documentation is here, with a great sample map here.
You could use PHP (or another script) to create a KML or GeoRSS file (much like Flickr's KML and GeoRSS feeds) and have the Google Maps API function GGeoXML load the file as an overlay on the map.
See Google's example code here: http://code.google.com/apis/maps/documentation/examples/geoxml-rss.html
That example is actually loading a live GeoRSS feed from Flickr.