Skip to content
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

Ticks view #80

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 46 additions & 80 deletions example/index.module.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,66 @@
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<meta name='viewport' content='initial-scale=1.0'>
<meta charset='utf-8'>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#controls {
position: absolute;
top: 100px;
left: 10px;
display: flex;
flex-direction: column;
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}

/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}

#controls {
position: absolute;
top: 100px;
left: 10px;
display: flex;
flex-direction: column;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBTdH3AFSWLD3SrgbNqTGoRsg3U6W0qAAg&libraries=geometry"></script>
<script
src='https://maps.googleapis.com/maps/api/js?key=AIzaSyBTdH3AFSWLD3SrgbNqTGoRsg3U6W0qAAg&libraries=geometry'></script>
</head>
<body>
<div id="map"></div>
<div id="controls">
<button id="start-with-points">Start With Initial Points</button>
<button id="start">Start</button>
<button id="end">End</button>
<button id="invert">Invert Color</button>
<div id='map'></div>
<div id='controls'>
<button id='start-with-points'>Start With Initial Points</button>
<button id='start'>Start</button>
<button id='end'>End</button>
<button id='invert'>Invert Color</button>
</div>
<script type="module">
const center = {lat: 41.70, lng: -71.47};
<script type='module'>
import MeasureTool from '../dist/gmaps-measuretool.esm.js';

const center = { lat: 50, lng: -47.373046875 };
const segments = [
{
"lat": 41.69547509615208,
"lng": -71.36993408203125
},
{
"lat": 41.434490308949215,
"lng": -71.3671875
"lat": 36.143180284242554,
"lng": -78.0691619265955
},
{
"lat": 41.50857729743936,
"lng": -71.20513916015625
"lat": 66.47820814385636,
"lng": -47.373046875
},
{
"lat": 41.66923209813446,
"lng": -71.18435546874998
},
{
"lat": 41.80245339578072,
"lng": -71.34365722656248
},
{
"lat": 41.910875425626266,
"lng": -71.39034912109373
},
{
"lat": 42.01299182987659,
"lng": -71.38760253906248
},
{
"lat": 42.01299182987659,
"lng": -71.80508300781248
},
{
"lat": 41.69957665997156,
"lng": -71.795654296875
},
{
"lat": 41.3566211272796,
"lng": -71.82156249999998
},
{
"lat": 41.37723402378496,
"lng": -71.49471923828123
},
{
"lat": 41.6154423246811,
"lng": -71.48529052734375
},
{

"lat": 41.69547509615208,
"lng": -71.36993408203125
"lat": 37.61423141542417,
"lng": -2.3291015625
}
];

var map, measureTool;

function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center,
zoom: 9,
zoom: 4,
scaleControl: true
});
measureTool = new MeasureTool(map, {
Expand Down Expand Up @@ -138,17 +103,18 @@
// measureTool.removeListener('measure_change');
});
}
initMap()

initMap();
let inverted = false;
document.querySelector('#start-with-points')
.addEventListener('click', () => measureTool.start(segments));
.addEventListener('click', () => measureTool.start(segments));
document.querySelector('#start')
.addEventListener('click', () => measureTool.start());
document.querySelector('#end')
.addEventListener('click', () => measureTool.end());
document.querySelector('#invert')
.addEventListener('click', () => {
inverted = !inverted
inverted = !inverted;
measureTool.setOption('invertColor', inverted);
});
</script>
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"homepage": "https://github.com/zhenyanghua/MeasureTool-GoogleMaps-V3#readme",
"dependencies": {
"d3-drag": "^2.0.0",
"d3-geo": "^2.0.1",
"d3-selection": "^2.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ const esm = {
onwarn
};

export default [umd, esm];
export default [esm];
30 changes: 30 additions & 0 deletions src/geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ export class Geometry {
return segments;
}

get lingString() {
return Geometry.toLineString(this._nodes);
}

static toLineString(points) {
return {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": points
}
}
}

constructor() {
this._nodes = [];
}
Expand All @@ -31,4 +45,20 @@ export class Geometry {
insertNode(i, point) {
this._nodes.splice(i, 0, point);
}

static equals(segments1, segments2) {
if (segments1.length !== segments2.length) {
return false;
}
for (let i = 0; i < segments1.length; i++) {
if (segments1[i][0][0] !== segments2[i][0][0] ||
segments1[i][0][1] !== segments2[i][0][1] ||
segments1[i][1][0] !== segments2[i][1][0] ||
segments1[i][1][1] !== segments2[i][1][1]) {
return false;
}
}
return true;
}

}
62 changes: 61 additions & 1 deletion src/helper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { UnitTypeId } from './UnitTypeId';
import { Geometry } from './geometry';
export default class Helper {
constructor(options) {
this._options = {
unit: UnitTypeId.METRIC,
};
Object.assign(this._options, options);
this._lastInterpolatedPoints = { segments: [], length: 0, points: [] };
this.init();
}

Expand Down Expand Up @@ -196,7 +198,7 @@ export default class Helper {
* @private
*/
static _interpolate(p1, p2, fraction) {
let point = google.maps.geometry.spherical.interpolate(
const point = google.maps.geometry.spherical.interpolate(
new google.maps.LatLng(p1[1], p1[0]),
new google.maps.LatLng(p2[1], p2[0]),
fraction
Expand All @@ -209,4 +211,62 @@ export default class Helper {
// let y = m * x + b;
// return [x, y];
}

/**
* if last segment length (start with 0) adds current segment length is less than tick length,
* segment length = last segment length + segment length
* last segment length = segment length;
* move on to the next segment
*
* current point = compute the latlng using fraction (tick length - last segment length ) / current segment length
* between current segment start point and end point.
* segment length = segment length - (tick length - last segment length)
*
* While the given minimum tick length is less than the segment length,
* compute the latlng at the fraction between current point and the end point.
* push the latlng
* assign the latlng to current point.
* segment length subtracts tick length.
*
* @param segments
* @param length
* @param includeSegmentNodes
*/
interpolatePointsOnPath(segments, length, includeSegmentNodes = false) {
if (this._lastInterpolatedPoints.length === length &&
Geometry.equals(this._lastInterpolatedPoints.segments, segments)) {
return this._lastInterpolatedPoints.points;
}
console.debug('interpolatePointsOnPath called')
if (segments.length === 0) return [];
let lastSegmentLength = 0, curPoint = segments[0][0], points = [];
for (let i = 0; i < segments.length; i++) {
if (includeSegmentNodes) points.push(segments[i][0]);
let segmentLength = this.computeLengthBetween(segments[i][0], segments[i][1]);
if (lastSegmentLength + segmentLength < length) {
segmentLength += lastSegmentLength;
lastSegmentLength = segmentLength;
continue;
}
curPoint = Helper._interpolate(
segments[i][0],
segments[i][1],
(length - lastSegmentLength) / segmentLength);
segmentLength -= (length - lastSegmentLength);
points.push(curPoint);
while(length < segmentLength) {
curPoint = Helper._interpolate(curPoint, segments[i][1], length / segmentLength);
points.push(curPoint);
segmentLength -= length;
lastSegmentLength = segmentLength;
}
}
if (includeSegmentNodes) points.push(segments[segments.length - 1][1]);
this._lastInterpolatedPoints = {
length,
segments,
points
};
return points;
}
}
Loading