-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
5587 - make geolocation show geolocation #5629
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
02261c9
adding svg layer...
maxgrossman b7f0d86
working on geolocate layer
maxgrossman b9ddfca
implement point/puck geolocation
maxgrossman b010f42
update layers test for new geolocate svg layer
maxgrossman 83c0843
just call accuracy function when setting r attr
maxgrossman 3755951
remove unused vars
maxgrossman 399e778
remove dispatched changes and re-order layers
maxgrossman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,137 @@ | ||
import { select as d3_select } from 'd3-selection'; | ||
|
||
import { svgPointTransform } from './helpers'; | ||
import { geoMetersToLat } from '../geo'; | ||
import _throttle from 'lodash-es/throttle'; | ||
|
||
export function svgGeolocate(projection, context, dispatch) { | ||
var layer = d3_select(null); | ||
var _position; | ||
|
||
|
||
function init() { | ||
if (svgGeolocate.initialized) return; // run once | ||
svgGeolocate.enabled = false; | ||
svgGeolocate.initialized = true; | ||
} | ||
|
||
function showLayer() { | ||
layer.style('display', 'block'); | ||
} | ||
|
||
|
||
function hideLayer() { | ||
layer | ||
.transition() | ||
.duration(250) | ||
.style('opacity', 0); | ||
} | ||
|
||
function layerOn() { | ||
layer | ||
.style('opacity', 0) | ||
.transition() | ||
.duration(250) | ||
.style('opacity', 1); | ||
|
||
} | ||
|
||
function layerOff() { | ||
layer.style('display', 'none'); | ||
} | ||
|
||
function transform(d) { | ||
return svgPointTransform(projection)(d); | ||
} | ||
|
||
function accuracy(accuracy, loc) { // converts accuracy to pixels... | ||
var degreesRadius = geoMetersToLat(accuracy), | ||
tangentLoc = [loc[0], loc[1] + degreesRadius], | ||
projectedTangent = projection(tangentLoc), | ||
projectedLoc = projection([loc[0], loc[1]]); | ||
|
||
// southern most point will have higher pixel value... | ||
return Math.round(projectedLoc[1] - projectedTangent[1]).toString(); | ||
} | ||
|
||
function update() { | ||
var geolocation = { loc: [_position.coords.longitude, _position.coords.latitude] }; | ||
|
||
var groups = layer.selectAll('.geolocations').selectAll('.geolocation') | ||
.data([geolocation]); | ||
|
||
groups.exit() | ||
.remove(); | ||
|
||
var pointsEnter = groups.enter() | ||
.append('g') | ||
.attr('class', 'geolocation'); | ||
|
||
pointsEnter | ||
.append('circle') | ||
.attr('id', 'geolocate-radius') | ||
.attr('dx', '0') | ||
.attr('dy', '0') | ||
.attr('fill', 'rgb(15,128,225)') | ||
.attr('fill-opacity', '0.3') | ||
.attr('r', '0'); | ||
|
||
pointsEnter | ||
.append('circle') | ||
.attr('dx', '0') | ||
.attr('dy', '0') | ||
.attr('fill', 'rgb(15,128,225)') | ||
.attr('stroke', 'white') | ||
.attr('stroke-width', '1.5') | ||
.attr('r', '6'); | ||
|
||
groups.merge(pointsEnter) | ||
.attr('transform', transform); | ||
|
||
d3_select('#geolocate-radius').attr('r', accuracy(_position.coords.accuracy, geolocation.loc)); | ||
} | ||
|
||
function drawLocation(selection) { | ||
var enabled = svgGeolocate.enabled; | ||
|
||
layer = selection.selectAll('.layer-geolocate') | ||
.data([0]); | ||
|
||
layer.exit() | ||
.remove(); | ||
|
||
var layerEnter = layer.enter() | ||
.append('g') | ||
.attr('class', 'layer-geolocate') | ||
.style('display', enabled ? 'block' : 'none'); | ||
|
||
layerEnter | ||
.append('g') | ||
.attr('class', 'geolocations'); | ||
|
||
layer = layerEnter | ||
.merge(layer); | ||
|
||
if (enabled) { | ||
update(); | ||
} else { | ||
layerOff(); | ||
} | ||
} | ||
|
||
drawLocation.enabled = function (position, enabled) { | ||
if (!arguments.length) return svgGeolocate.enabled; | ||
_position = position; | ||
svgGeolocate.enabled = enabled; | ||
if (svgGeolocate.enabled) { | ||
showLayer(); | ||
layerOn(); | ||
} else { | ||
hideLayer(); | ||
} | ||
return this; | ||
}; | ||
|
||
init(); | ||
return drawLocation; | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than just appending it here, I think it might make more sense to insert it before the touch layer, so that clicks on osm objects will still work. something like:insert('g', '.data-layer.touch')
Forget what I said here.. If
layer-geolocate
is beforelayer-touch
you can append the circle to it.