Refactor mapview javascript