Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit e2bc20b

Browse files
committed
Move to_screen_location to src/third_party and add proper licensing info.
1 parent 316f2d0 commit e2bc20b

File tree

7 files changed

+104
-32
lines changed

7 files changed

+104
-32
lines changed

packages/google_maps_flutter/google_maps_flutter_web/LICENSE

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
google_maps_flutter_web
2+
13
Copyright 2013 The Flutter Authors. All rights reserved.
24

35
Redistribution and use in source and binary forms, with or without modification,
@@ -23,3 +25,27 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2325
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2426
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2527
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
--------------------------------------------------------------------------------
29+
to_screen_location
30+
31+
The MIT License (MIT)
32+
33+
Copyright (c) 2008 Krasimir Tsonev
34+
35+
Permission is hereby granted, free of charge, to any person obtaining a copy
36+
of this software and associated documentation files (the "Software"), to deal
37+
in the Software without restriction, including without limitation the rights
38+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
39+
copies of the Software, and to permit persons to whom the Software is
40+
furnished to do so, subject to the following conditions:
41+
42+
The above copyright notice and this permission notice shall be included in all
43+
copies or substantial portions of the Software.
44+
45+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
48+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
50+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
51+
SOFTWARE.

packages/google_maps_flutter/google_maps_flutter_web/lib/google_maps_flutter_web.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platf
2525
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
2626
import 'package:google_maps/google_maps.dart' as gmaps;
2727

28+
import 'src/third_party/to_screen_location/to_screen_location.dart';
2829
import 'src/types.dart';
2930

3031
part 'src/google_maps_flutter_web.dart';

packages/google_maps_flutter/google_maps_flutter_web/lib/src/convert.dart

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,6 @@ void _applyCameraUpdate(gmaps.GMap map, CameraUpdate update) {
416416
}
417417

418418
// original JS by: Byron Singh (https://stackoverflow.com/a/30541162)
419-
// (This is the reverse of [_latLngToPixel])
420419
gmaps.LatLng _pixelToLatLng(gmaps.GMap map, int x, int y) {
421420
final bounds = map.bounds;
422421
final projection = map.projection;
@@ -442,33 +441,3 @@ gmaps.LatLng _pixelToLatLng(gmaps.GMap map, int x, int y) {
442441

443442
return projection.fromPointToLatLng!(point)!;
444443
}
445-
446-
// original JS by: Krasimir (https://krasimirtsonev.com/blog/article/google-maps-api-v3-convert-latlng-object-to-actual-pixels-point-object)
447-
// (This is the reverse of [_pixelToLatLng])
448-
gmaps.Point _latLngToPixel(gmaps.GMap map, gmaps.LatLng coords) {
449-
final zoom = map.zoom;
450-
final bounds = map.bounds;
451-
final projection = map.projection;
452-
453-
assert(
454-
bounds != null, 'Map Bounds required to compute screen x/y of LatLng.');
455-
assert(projection != null,
456-
'Map Projection required to compute screen x/y of LatLng.');
457-
assert(zoom != null,
458-
'Current map zoom level required to compute screen x/y of LatLng.');
459-
460-
final ne = bounds!.northEast;
461-
final sw = bounds.southWest;
462-
463-
final topRight = projection!.fromLatLngToPoint!(ne)!;
464-
final bottomLeft = projection.fromLatLngToPoint!(sw)!;
465-
466-
final scale = 1 << (zoom!.toInt()); // 2 ^ zoom
467-
468-
final worldPoint = projection.fromLatLngToPoint!(coords)!;
469-
470-
return gmaps.Point(
471-
((worldPoint.x! - bottomLeft.x!) * scale).toInt(),
472-
((worldPoint.y! - topRight.y!) * scale).toInt(),
473-
);
474-
}

packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class GoogleMapController {
319319
assert(_googleMap != null,
320320
'Cannot get the screen coordinates with a null map.');
321321

322-
final point = _latLngToPixel(_googleMap!, _latLngToGmLatLng(latLng));
322+
final point = toScreenLocation(_googleMap!, _latLngToGmLatLng(latLng));
323323

324324
return ScreenCoordinate(x: point.x!.toInt(), y: point.y!.toInt());
325325
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2008 Krasimir Tsonev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# to_screen_location
2+
3+
The code in this directory is a Dart re-implementation of Krasimir Tsonev's blog
4+
post: [GoogleMaps API v3: convert LatLng object to actual pixels][blog-post].
5+
6+
The blog post describes a way to implement the [`toScreenLocation` method][method]
7+
of the Google Maps Platform SDK for the web.
8+
9+
Used under license (MIT), [available here][blog-license], and in the accompanying
10+
LICENSE file.
11+
12+
[blog-license]: https://krasimirtsonev.com/license
13+
[blog-post]: https://krasimirtsonev.com/blog/article/google-maps-api-v3-convert-latlng-object-to-actual-pixels-point-object
14+
[method]: https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/Projection#toScreenLocation(com.google.android.libraries.maps.model.LatLng)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2008 Krasimir Tsonev
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
import 'package:google_maps/google_maps.dart' as gmaps;
24+
25+
/// Returns a screen location that corresponds to a geographical coordinate ([gmaps.LatLng]).
26+
///
27+
/// The screen location is in pixels relative to the top left of the Map widget
28+
/// (not of the whole screen/app).
29+
///
30+
/// See: https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/Projection#public-point-toscreenlocation-latlng-location
31+
gmaps.Point toScreenLocation(gmaps.GMap map, gmaps.LatLng coords) {
32+
final zoom = map.zoom;
33+
final bounds = map.bounds;
34+
final projection = map.projection;
35+
36+
assert(
37+
bounds != null, 'Map Bounds required to compute screen x/y of LatLng.');
38+
assert(projection != null,
39+
'Map Projection required to compute screen x/y of LatLng.');
40+
assert(zoom != null,
41+
'Current map zoom level required to compute screen x/y of LatLng.');
42+
43+
final ne = bounds!.northEast;
44+
final sw = bounds.southWest;
45+
46+
final topRight = projection!.fromLatLngToPoint!(ne)!;
47+
final bottomLeft = projection.fromLatLngToPoint!(sw)!;
48+
49+
final scale = 1 << (zoom!.toInt()); // 2 ^ zoom
50+
51+
final worldPoint = projection.fromLatLngToPoint!(coords)!;
52+
53+
return gmaps.Point(
54+
((worldPoint.x! - bottomLeft.x!) * scale).toInt(),
55+
((worldPoint.y! - topRight.y!) * scale).toInt(),
56+
);
57+
}

0 commit comments

Comments
 (0)