I'm doing an app in my app. I'm using google maps flutter and I'm having an issue regarding scrolling. When I scroll the map I want to get the center points (long, lat) of the map that is if I scroll the map and stop at certain place it would grab the location points of center.
Can anyone suggest how to solve this issue? It can be very helpful Thank you in advance..
I would get the bounds of the screen, and then find the center of those bounds. If you have a GoogleMapController for the GoogleMap, just use a function like this:
getCenter() async {
LatLngBounds bounds = await mapsController.getVisibleRegion();
LatLng center = LatLng(
(bounds.northeast.latitude + bounds.southwest.latitude) / 2,
(bounds.northeast.longitude + bounds.southwest.longitude) / 2,
);
return center;
}
In the latest version of google maps flutter we can get the coordinates of location by passing in the screen coordinatinates,
// some code here
Size screen = MediaQuery.of(context).size;
// some code here
final coords = await mapController.getLatLng(ScreenCoordinate( x: screen.width/2, y:
screen.width/2));
Related
I have a project that uses the "flutter_painter" library to draw on top of an image, the problem is that when I zoom in on the image, the coordinates are still from the original image and not with the zoom applied, I need to map these coordinates to draw under the zoomed image, and when you unzoom it stays in the drawn location.
I use the "onSecondaryTapUp: (details){}" from the "GestureDetector" widget around the "Flutter_Painter" library to get the click position on the image.
try transformationController.toScene(update.localPosition);
_onDragUpdate(BuildContext context, DragUpdateDetails update) {
RenderBox getBox = context.findRenderObject() as RenderBox;
var local = getBox.globalToLocal(update.globalPosition);
final Offset scenePoint = transformationController.toScene(update.localPosition);
print("Update Point");
debugPrint("local: $local");
debugPrint("scenePoint: $scenePoint");
}
So I've been trying really hard to crop an image according to my needs in flutter.
Problem statement:
I have a screen and in that screen I show a frame while the device camera is run on the background. Now, what I want is that whenever the user clicks a photo, only the area of that image inside the frame should be kept and rest should be cropped.
What I have done so far?
Added a package image 3.1.3
Wrote code to fetch x,y coordinates of my frame.
Using the calculated x,y coordinates and copyCrop method from the Image package to crop the clicked image.
Now the problem is that I do not know how copyCrop works and the code right now does not give me the expected results.
final GlobalKey _key = GlobalKey();
void _getOffset(GlobalKey key) {
RenderBox? box = key.currentContext?.findRenderObject() as RenderBox?;
Offset? position = box?.localToGlobal(Offset.zero);
if (position != null) {
setState(() {
_x = position.dx;
_y = position.dy;
});
}
}
I assign this _key to my Image.file(srcToFrameImage) and the function above yields 10, 289.125
Here 10 is the offset from x and 289.125 is the offset from y. I used this tutorial for the same.
Code to crop my image using the Image package:
var bytes = await File(pictureFile!.path).readAsBytes();
img.Image src = img.decodeImage(bytes)!;
img.Image destImage = img.copyCrop(
src, _x!.toInt(), _y!.toInt(), src.width, src.height);
var jpg = img.encodeJpg(destImage);
await File(pictureFile!.path).writeAsBytes(jpg);
bloc.addFrontImage(File(pictureFile!.path));
Now, can anyone tell me how i can do this effectively? Right now, it does crop my image but not as I want it to be. It would be really great if someone could tell me how does copyCrop work and what is the meaning of all these different parameters that we pass into it.
Any help would be appreciated.
Edit:
Now, as you can see, i only want the image between this frame to be kept after being captured and rest to be cropped off.
If I pass latlnglastPosition to my widget, it's fine. I can see latitude and longitude and the value changes with zero lag as my camera position changes. However, using anything more than that causes crazy lag. It's practically unusable. Does anybody know what's causing this or if there's a better way of doing this? I'm trying to display latitude, longitude, altitude, distance from center versus current position in meters and miles. Here's the code:
void _onCameraMove(CameraPosition cameraPosition) async {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
lat = position.latitude;
lng = position.longitude;
alt = position.altitude;
_distancebetween = await Geolocator().distanceBetween(
_lastMapPosition.latitude, _lastMapPosition.longitude, lat, lng);
distancebetweenFinal = _distancebetween.toStringAsFixed(2);
distancebetweenFinalMiles = _distancebetween * (0.000621371);
_lastMapPosition = cameraPosition.target;
var latlnglastPosition = _lastMapPosition;
_currentZoom = cameraPosition.zoom;
setState(() {
latlnglastPosition;
distancebetweenFinal;
distancebetweenFinalMiles;
_distancebetween;
_lastMapPosition;
});
}
This is using Flutter Google Maps.
Edit: I removed the conversion from meters to miles. It didn't help. I don't think the altitude is having any impact. It's just going from current camera view position to my current position that's delaying everything. Is there perhaps a way to increase the frequency i receive location info? maybe that's hanging things up.... ?
Edit: or maybe I can use get last known position when a new location is not available? Not sure if that's the best way to handle this
Edit: I set lat = 34.0; lng = 34.0; and it's super fast now. However, it doesn't solve my problem. I need lat and lng to reflect my current position, and it seems i'm either restricted in pulling my current position or it's too costly to pull it as frequently as I need to. I don't know the best way to handle this... Any ideas are appreciated.
I got it (I think):
I replaced
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
with:
Position position = await Geolocator().getLastKnownPosition();
I think position.latitude and position.longitude were too costly before. So, I'm getting the last known position. If anyone knows a better way, please let me know.
I know how to move the zoomable map to a specific lat/long, and that point will be centered in the screen. If the user moves the map (drags it sideways), how do I get the coordinates at the point of the map that is now centered?
Thanks!
I would recommend to use the Camera.ScreenToWorldPoint method from Unity:
https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html?_ga=2.95757022.39200391.1587116665-1246420375.1587116665
Please also have a look at this similar thread:
https://forum.unity.com/threads/how-to-get-a-world-position-from-the-center-of-the-screen.524573/
Thanks!
Based on the similar thread you pointed me to, I added a function to my QuadTreeCameraMovement...
public Vector2d GeoCoordsAtCenter() {
var centerScreen = _referenceCamera.ViewportToScreenPoint(new Vector3(.5f, .5f, _referenceCamera.transform.localPosition.y));
var pos = _referenceCamera.ScreenToWorldPoint(centerScreen);
var latlongDelta = _mapManager.WorldToGeoPosition(pos);
Debug.Log("CENTER: Latitude: " + latlongDelta.x + " Longitude: " + latlongDelta.y);
return latlongDelta;
}
Im trying to place a marker on the position i am, this way:
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_container)).getMap();
googleMap.setMyLocationEnabled(true);
My question is, how to get coordinates from myLocation, i tried, googleMap.getMyLocation().getLatitude() and googleMap.getMyLocation().getAltitude() right after googleMap.setMyLocationEnabled(true), but app crashes, another thing i did was Location loc=lm.getLastKnownLocation(provider) but are not the same coordinates and the marker is placed in the wrong place.
How can you people help me?
SupportMapFragment mf =(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mf.getMap();
mMap.setMyLocationEnabled(true);
mMap.setMapType(mMap.MAP_TYPE_NORMAL);
Just add these two lines of code to get current location on Maps
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
and when you long press on any location on map add this code to get its location
googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng arg0) {
double alt = arg0.latitude;
double alo = arg0.longitude;
MarkerOptions marker1 = new MarkerOptions().position(new LatLng(alt, alo)).title("Lat ="+alt+" Lang="+alo);
googleMap.addMarker(marker1);
}
});