forked from goakley/nyt-openhack-tcnjay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
75 lines (72 loc) · 2.54 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var current_value = $("#form_location_text").val();
if (current_value.length == 0) {
$("#form_location_text").val(position.coords.latitude + "," +
position.coords.longitude);
}
});
}
var map = new google.maps.Map(document.getElementById('map'),
{center:(new google.maps.LatLng(0,0)),
zoom:3,
mapTypeId:google.maps.MapTypeId.HYBRID});
google.maps.event.trigger(map, 'resize', function(){});
$("#form_location").submit(function(event) {
$("#form_location").children('input[type=submit]').attr('disabled',
'disabled');
var input = $("#form_location_text").val();
$("#form_location_text").val("");
event.preventDefault();
$("#notif").text("Please Wait...");
MusicMap.extractLocation(input, function(lat, lon) {
var latlon = new google.maps.LatLng(lat,lon);
var circle = new google.maps.Circle({center:latlon,
fillColor:'red', fillOpacity:0.1,
strokeWeight:1,
radius:111120,
map:map});
map.setCenter(latlon);
map.setZoom(8);
EchoServer.obtainArtists(lat, lon, function(artists) {
$("#notif").html(" ");
for (var i = 0; i < artists.length; i++) {
console.log(artists[i].name + " " + artists[i].lat + " " + artists[i].lon);
var marker = new google.maps.Marker({
position:(new google.maps.LatLng(artists[i].lat,
artists[i].lon)),
title:artists[i].name,
clickable:true,
map:map
});
marker.artist = artists[i];
google.maps.event.addListener(marker, 'click', function(event){
var info = "<h1>"+this.artist.name+" (" +
this.artist.artist_location.location + ")</h1>";
info += "<ul>"
for (var i = 0; i < this.artist.songs.length; i++)
info += "<li>" + this.artist.songs[i].title + "</li>";
info += "</ul>";
$("#songs").html(info);
event.stop();
});
}
$("#form_location").children('input[type=submit]').removeAttr('disabled');
});
});
});
MusicMap = {};
MusicMap.extractLocation = function(input, callback) {
var regex = new RegExp("[-+]?[0-9]*\.?[0-9]+,[-+]?[0-9]*\.?[0-9]+");
var parsed_input = input.match(regex);
if (parsed_input) {
parsed_input = parsed_input[0].split(",");
callback(parsed_input[0], parsed_input[1]);
} else {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':input}, function(results, status) {
callback(results[0].geometry.location.lat(),
results[0].geometry.location.lng());
});
}
};