How to remove all pins Google Maps V3? - ajax

I am currently working on a code where we have a world map. On this map, the users position (you) is marked with a white marker. And there is so called "drivers" which drive around on our globe. The drivers position is taken from an API which my friend has written. But now to the problem, when our site here grabs values from all drivers which is shown on the API, it add their position. Then after 3000 ms, it grabs values again. Which means that drivers pins adds again, again and again, over each other. That due to my loop which grabs values from our API every 3000 ms. How could I use the code I got now, and make all drivers pins to be removed 100 ms before values are grabbed from my API?
Demo: http://profusum.se/bc/map/ (Notice that the pins are getting "greener")
jsFiddle: http://goo.gl/8fQS1 (Shorted URL to "jsFiddle=zMYSw"
NOTICE
My jsFiddle may not work, if so, please comment and I will update this question.

I would recommend storing a reference to each "driver" in an array and then either call setMap(null) on each one at the start of your handleData() function, or modify your handleData() function to just update the position of each one.

Related

Get all postcodes on a given location using any Google API

Is it possible to get all postal codes in a given location inside the given radius?
What Google API should I use?
Example: I have a lat. and a long. and my radius is 15kms. How do I get the post codes of the areas inside the radius?
I'm kinda new to using API's and Google API's.
Thanks!
This is a process called "Reverse Geocoding" Google offers it here - https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse
However pretty sure that Google's API will try to give you one closest result rather than many.
You can do this with APIs (often paid for) however, be warned that you have to be quite careful with the radius element, setting it too small in rural areas will bring back 0 results and setting it too big in urban areas will bring back potentially thousands.

Order posts by number manually entered by the user

I have a website which its admin user logs into a restricted area and adds content that is displayed on the public area. The admin wants a simple input text field on the post insert/edit form where he can type a number that determines in which position that post will be displayed to the public.
So, if the “Foo” post is given the number 3, it should be listed in the 3rd position, the old post 3 should become 4, 4 should become 5, and so on. If post 5 is repositioned to 1, 1 should automatically become 2, 2 should become 3, etc. If the admin reposition, 3 to 2, 2 should just become 3 without the need to change the other ones. If 10 becomes 9, 9 becomes 10 and no need to change other posts' position either. In short, when the position of one single post is changed (or inserted in the middle of existing posts), the remaining posts should have their position shifted accordingly.
I have considered two possibilities:
create a crazy algorithm that selects each post needed and updates the position column;
implement a linked list in the table so that each post always knows what is the primary key of the next one.
With the first option, when the admin inserts or updates a post's position, all the work is done at that moment, and when the public site is visited, the SQL just selects all the needed posts in a single query and order them by position. I don't really know yet how I would implement it, but I'm sure using a lot of code and possibly bad trade-offs it would be possible.
With the second option, there is much less work done during the insert/update, but when users access the site, I need to do one select for each post so that the right positioning can be achieved using the each post's link to the next “node”. So, if I have 150 posts in a given category, I'll have to run 150 selects (one for each post that is pointed to by the current "node". No pagination will be used in this case, and only the titles will be really listed, one below the other, so, I will really list them all.
I would like suggestions to other approaches, insights or improvements on the two possibilities I have described.
SOLUTION
I accepted #FractlizeR answer because it uses some other established piece of software to support his choice.
I wrote a small prototype on the solution I ended up using at work. Of course the code I used at work is different because I was changing a legacy system. Still, I used exactly the same SQL approach as the ones in the prototype. I hope I can improve that prototype and specially its comments soon, but at least it is there in case I or someone else needs to do something similar.
I would vote for #1. This is what, for example, XenForo forum engine does. It uses post position for paging and other things. When it needs to insert a new post into a thread, it queries thread for all posts, that needs to be reordered, then for each post calculates a new position and then updates table for all posts, which position changed.
We use XenForo for a long time in production and actually there should be no problems involved since this operation (reorder) is not so complex.

Multiple groups overlapping - SketchUp Ruby API

I'm probably missing the dangers to this technique since I'm new to the Ruby API (and SketchUp in general), but basically I'm using loops in my project to travel upwards along the z-axis creating individual cylinders stacked one after another.
Now, I'm calling a function (once) which has a for-loop within it that performs this task, but since I need the entire stack to be packed as a single group, I've passed to that function a group initialized within the calling function. Then, the function that generates the stack makes a temporary group and every new cylinder created is made a part of that group. Finally, once the function is finished, it assigns the group it recieved as a parameter to that temporary group.
So, the problem is when I generate the model in SketchUp, and I click on the stack to perform an action on it, like erase or hide, I have to right click the group and hit the action at least two times before it actually performs it. What is the proper of way of grouping each cylinder so that this doesn't occur?
Here is a sample source code outlining this method: http://codepad.org/E3a1xXVG
Thanks.
UPDATE 1: http://i.stack.imgur.com/ejIuB.png This screenshot is a frame-by-frame veiw of the problem occuring in the model Sketchup generates from the sample code above. Right clicking the group and selecting an action must be done twice before taking effect. I'm trying to fix the sample code so that an action need only be selected once (as it should behave normally) before I can apply it to my much larger project - which utilizes the same method.

LocalStorage, several Ajax requests or huge Ajax request?

I'm facing a really huge issue (at least for me). I'll give you some background.
I'm developing a mobile web app which will show information about bus stations and bike stations in my city. It will show GMap markers for both bus and bike stops and, if you click it, you will have info about the arrival time for the bus or how many bikes are available. I have that covered pretty nicely.
The problematic part is loading the stations.
My first approach was to load the whole amount of stations at page loading. It is about 200 Kb of JSON plus the time it gets to iterate through the array and put it in the map. They are hiddenly loaded so, when the users click on the line name, they appear using the 'findMarker' function. If other stations were present at the map, they get hidden to avoid having too many markers in the map.
This worked good with new mobiles such as iPhone 4 or brand new HTC but it doesn't perform good with 2 years old mobiles.
The second approach I thought, was to load them by request, so you click on the station, they are loaded to the map, and then shown but this leads to two problems:
The first is that you may be (or may be not) perform several requests
which may ends the same (?)
The second one is that to avoid having so
many markers, they should be hidden or deleted so you may be deleting
info that should be needed again in a while, like bike stations that
are loaded as a group and not by line.
Finally, I thought about using LocalStorage to store them, as they won't change that much but will be a huge amount of data and then, a pain in the ass to retrieve them as they are key-value, and also (and I'm not really sure about that) I could find devices with no support of this feature having to fallback to one of the other options.
So, that said, I thought that may be someone faced some similar problem and solved it in some way or have some tips for me :).
Any help would be really appreciated.
The best approach depends on the behaviour of your users. Do they typically click on a lot of stations or just a few? Do they prefer a faster startup (on-demand loading) or a more responsive station detail display (pre-loading data)?
An approach worth investigating would be to load the data by request but employ browser caching (ETag and Expires headers) to avoid retrieving the same information over and over again. This would solve both your concerns without dealing with LocalStorage.
See this this question for different approaches for browser caching ETag vs Header Expires

How can you rotate banner ads using CouchApp & CouchDB?

For context: this is an HTML app, with little or no browser side JavaScript. I can't easily change that so need to do this on the server.
CouchDB is built to not have side effects. This is fair enough. But there seems to be no method that i can conceive of with shows, views, lists to change what is shown to a user with subsequent requests, or based on user objects, without writing data.
And can a get request for document result in the creation of a new record? Im guessing not as that would be a side effect.
But if you can, you could just create a log and then have a view that picks an advert firm a set of documents describing adverts which is affected by the change in the log when a previous ad was shown.
I'm not actually going to show adverts on my site, I'm going to have tips, and article summaries and minor features that vary from page load to page load.
Any suggestions appreciated.
I've wrapped my head around how to work with the grain for the rest of the functionality I need, but this bit seems contrary to the way couchdb works.
I think you're going to need a list function that receives a set of documents from the view and then chooses only one to return, either at random or some other method. However, because you're inside a list function you gain access to the user's request details, including cookies (which you can also set, btw.) That sounds more like what you want.
In addition, you could specify different Views for the list function to use at query-time. This means you could, say, have only random articles show up on the homepage, but any type of content show up on all others.
Note: You can't get access to the request in a map/reduce function and you'll run into problems if you do something like Math.random() inside a map function.
So a list function is the way to go.
http://guide.couchdb.org/draft/transforming.html
Look into the various methods of selecting a random document from a view. That should enable you to choose a random document (presumably representing an ad, tip, etc.) to display.

Resources