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

Convert point to latitude and longitude #496

Closed
HosseinBabriani opened this issue Jan 13, 2020 · 10 comments
Closed

Convert point to latitude and longitude #496

HosseinBabriani opened this issue Jan 13, 2020 · 10 comments

Comments

@HosseinBabriani
Copy link

Hi i need latitude and longitude inside getTileUrl function but I can't usemap.unproject or crs.pointToLatLng but result isn't real result.
Is there any way to solve my problem ?

@moovida
Copy link
Contributor

moovida commented Jan 13, 2020

@hossein-hbb not sure I understand. You can't use the mapstate functions because you do not have access to it? And therefore you need a conversion between the TMS tileschema and lat/long?

@HosseinBabriani
Copy link
Author

I try to modify plugin and add my custom parameters in plugin like this:
'lat':lat.toString(),
'lng':lng.toString(),

@HosseinBabriani
Copy link
Author

But when I use pointToLatLng or usemap.unproject return wrong values for lat and lng...

@HosseinBabriani
Copy link
Author

HosseinBabriani commented Jan 13, 2020

I need change methode like this :

 String getTileUrl(Coords coords, TileLayerOptions options,LatLng latLng) {
    var data = <String, String>{
      'x': coords.x.round().toString(),
      'y': coords.y.round().toString(),
      'z': coords.z.round().toString(),
      'lat':latLng.latitude.toString(),
      'lng':latLng.longitude.toString(),
      's': getSubdomain(coords, options)
    };
    if (options.tms) {
      data['y'] = invertY(coords.y.round(), coords.z.round()).toString();
    }
    var allOpts = Map<String, String>.from(data)
      ..addAll(options.additionalOptions);
    return util.template(options.urlTemplate, allOpts);
  }

@moovida
Copy link
Contributor

moovida commented Jan 13, 2020

It might be you are missing some pieces in the conversion.
Here some code I use to convert a screen position to latlong.

var p = e.localPosition;
var pixelBounds = map.getLastPixelBounds();

CustomPoint pixelOrigin = map.getPixelOrigin();
var ll = map.unproject(CustomPoint(pixelOrigin.x + p.dx, pixelOrigin.y + p.dy));

As you can see you need to consider the pixelOrigin also. Might that be your problem?

@HosseinBabriani
Copy link
Author

Can you explain e.localPosition .
how can I access or create e?

@moovida
Copy link
Contributor

moovida commented Jan 13, 2020

In my case I had an event from a GestureDetector. That part will not be of use for you, since you are converting tiles positions. I wanted to show you more the pixelOrigin part.

I am starting to have the feeling that you just need to convert the tile position index to lat/long?
If that is so, you can just use the following methods:

  static double tile2lon(int x, int z) {
    return x / pow(2.0, z) * 360.0 - 180.0;
  }

  static double tile2lat(int y, int z) {
    double n = pi - (2.0 * pi * y) / pow(2.0, z);
    return radianToDeg(atan(sinh(n)));
  }

Rationale behind it is found here: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames

@HosseinBabriani
Copy link
Author

HosseinBabriani commented Jan 13, 2020

Its work but when _createTileWidget all tiles positions is wrong...
screenshot-1578915117768

my code :

  static double tile2lon(int x, int z) {
    return x / pow(2.0, z) * 360.0 - 180.0;
  }

  static double tile2lat(int y, int z) {
    var n = pi - (2.0 * pi * y) / pow(2.0, z);
    return radianToDeg(atan(numerics.sinh(n)));
  }

  String getTileUrl(Coords coords, TileLayerOptions options) {
    var data = <String, String>{
      'x': coords.x.round().toString(),
      'y': coords.y.round().toString(),
      'z': coords.z.round().toString(),
      'lat':tile2lat(coords.y.round(),coords.z.round()).toString(),
      'lng':tile2lon(coords.x.round(),coords.z.round()).toString(),
      's': getSubdomain(coords, options)
    };
    if (options.tms) {
      data['y'] = invertY(coords.y.round(), coords.z.round()).toString();
    }
    var allOpts = Map<String, String>.from(data)
      ..addAll(options.additionalOptions);
    return util.template(options.urlTemplate, allOpts);
  }

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Mar 28, 2021
@github-actions
Copy link

github-actions bot commented Apr 2, 2021

This issue was closed because it has been stalled for 5 days with no activity.

@github-actions github-actions bot closed this as completed Apr 2, 2021
JaffaKetchup pushed a commit that referenced this issue May 20, 2022
…nt (#1115)

* Added MapController.pointToLatLng() to get LatLng of given screen point

Ref #496, ref #607, ref #981, ref #1010

* Better pointToLatLng example

* dartfmt

* Fix zoom breaking pointToLatLng()

* Updated example to help fix the rotation issue

* Rebased on latest

* Fix pointToLatLng with rotation

* Fix trailing lines (3x attempts)

Co-authored-by: Polo <gitpjp@pm.me> and ibrierley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants