Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
html, body { | ||
margin: 0; | ||
padding: 0; | ||
width: 100%; | ||
height: 100%; | ||
} | ||
#map { | ||
width: 100%; | ||
height: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
;(function(){ | ||
|
||
Pusher.log = function(msg) { | ||
if(window.console && window.console.log) { | ||
window.console.log(msg); | ||
} | ||
}; | ||
|
||
var pusher = new Pusher(APP_KEY); | ||
pusher.connection.bind('state_change', function(state) { | ||
if( state.current === 'connected' ) { | ||
navigator.geolocation.getCurrentPosition(positionSuccess, positionError); | ||
} | ||
}); | ||
|
||
var channel = pusher.subscribe('visitor-hits'); | ||
channel.bind('new_hit', newHitReceived); | ||
|
||
function newHitReceived(data) { | ||
var latLng = new google.maps.LatLng(data.lat, data.lng); | ||
|
||
var styleMaker = new StyledMarker({ | ||
styleIcon: new StyledIcon( | ||
StyledIconTypes.BUBBLE, | ||
{ | ||
color:"fff", | ||
text:data.locality | ||
}), | ||
position: latLng, | ||
map: map | ||
}); | ||
} | ||
|
||
var visitorPosition = null; | ||
function positionSuccess(position) { | ||
visitorPosition = position; | ||
var lat = visitorPosition.coords.latitude; | ||
var lng = visitorPosition.coords.longitude; | ||
|
||
var location = new google.maps.LatLng(lat, lng); | ||
var geocoder = new google.maps.Geocoder(); | ||
geocoder.geocode({location: location}, function(response, status) { | ||
geocodeResponse.call(this, response, status, location) | ||
}); | ||
|
||
createMap(); | ||
} | ||
|
||
function geocodeResponse(response, status, location) { | ||
var locality = null; | ||
if(status == google.maps.GeocoderStatus.OK) { | ||
locality = findLocality(response); | ||
} | ||
|
||
if(locality != null) { | ||
$.ajax({ | ||
url: 'pusher/new_hit.php', | ||
type: 'post', | ||
data: { | ||
locality: locality.formatted_address, | ||
lat: visitorPosition.coords.latitude, | ||
lng: visitorPosition.coords.longitude | ||
}, | ||
success: function(data, textStatus, jqXHR) {}, | ||
error: function (jqXHR, textStatus, errorThrown) { alert('error'); } | ||
}); | ||
} | ||
} | ||
|
||
function findLocality(addresses) { | ||
if(addresses.length == 0) { | ||
return null; | ||
} | ||
|
||
for(var addrIndex = 0, addrCount = addresses.length; addrIndex < addrCount; ++addrIndex) { | ||
var address = addresses[addrIndex]; | ||
for(var typeIndex = 0, typeCount = address.types.length; typeIndex < typeCount; ++typeIndex) { | ||
if(address.types[typeIndex] == "locality") { | ||
return address; | ||
} | ||
} | ||
} | ||
|
||
return addresses[0]; | ||
} | ||
|
||
function positionError(error) { | ||
alert('position error'); | ||
} | ||
|
||
var map = null; | ||
function createMap() { | ||
var visitorLocation = | ||
new google.maps.LatLng( | ||
visitorPosition.coords.latitude, | ||
visitorPosition.coords.longitude); | ||
|
||
var mapOptions = { | ||
zoom: 2, | ||
center: visitorLocation, | ||
mapTypeId: google.maps.MapTypeId.ROADMAP | ||
}; | ||
|
||
map = new google.maps.Map(document.getElementById("map"), mapOptions); | ||
} | ||
|
||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
<?php | ||
require_once('../config.php'); | ||
require_once('Pusher.php'); | ||
|
||
$data = array( | ||
'lat' => $_POST['lat'], | ||
'lng' => $_POST['lng'], | ||
'locality' => $_POST['locality'] | ||
); | ||
|
||
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID); | ||
$pusher->trigger('visitor-hits', 'new_hit', $data); | ||
|
||
header('Content-type: application/json'); | ||
echo( json_encode($data) ); | ||
?> |