How to show current location indication - flutter

I'm using the Flutter SDK Version 4.3.1.0.
I get my location updates from the geolocator plugin and want to show the location on the HERE map. It correctly centers the map to the current location but there is no location indicator.
I currently use the following code. What else do I need?
void _showPosition(final GeoCoordinates coordinates) {
_mapView.mapScene.loadSceneForMapScheme(MapScheme.greyDay, (MapError error) {
if (error != null) {
print("Map scene not loaded. MapError: " + error.toString());
return;
}
_mapView.camera.lookAtPointWithDistance(coordinates, 1000);
});
}
I'm using the Flutter Channel beta, v1.17.0-3.4.pre

The HERE SDK for Flutter does not contain a pre-configured location indicator. You can easily create one by adding a circle item onto the map:
For this I would recommend using a MapPolygon that contains a GeoCircle shape. You can then also update the radius of the GeoCircle on the fly to indicate the current horizontal accuracy of the geolocator plugin.

Related

How to detect pan gesture in map with Flutter Here SDK

I'm building an app with Flutter and am trying to listen to the pan gestures of the map to get the center of the map view and place a pin on the center. The code I've tried is as follows:
void _setPanGestureHandler({HereMapController mapController}) {
_hereMapController.gestures.panListener = PanListener.fromLambdas(
lambda_onPan: (GestureState gestureState, Point2D panStartPoint,
Point2D panEndPoint, double panVelocity) {
if (gestureState == GestureState.begin) {
print("start pan");
} else if (gestureState == GestureState.end) {
var centerPointLat = _hereMapController.viewportSize.width / 2;
var centerPointLong = _hereMapController.viewportSize.height / 2;
GeoCoordinates geoCoordinates = _hereMapController
.viewToGeoCoordinates(Point2D(centerPointLat, centerPointLong));
if (geoCoordinates == null) {
return;
}
_addPoiMapMarker(geoCoordinates);
_getAddressForCoordinates(geoCoordinates);
} else if (gestureState == GestureState.update) {
print("pan updated");
} else if (gestureState == GestureState.cancel) {
print("pan cancelled");
}
});
}
The code is part of the search_app example and I've just added a panGesture listener to it.
On panning, I get the following in the debug console
W/PanGestureDetector(21446): [WARN ] PanGestureDetector - Invalid panning of zero duration but nonzero length, skipping
W/PanGestureDetector(21446): [WARN ] PanGestureDetector - Invalid panning of zero duration but nonzero length, skipping
Please let me know how I can solve this problem.
The code snippet from your question is not part of the search_app example, so I assume it is your code. However, what you are trying to do will not work properly:
_hereMapController.viewportSize is giving you the size in pixels of the map view. It is not providing geographic coordinates. Although you can use viewToGeoCoordinates() to convert pixel points to geographic coordinates, there is an easier way: The MapCamera gives you always the current center location for free via the targetCoordinates property.
The pan gesture handler is executed multiple times. It would not be advisable to call _addPoiMapMarker() each time the GestureState.end event occurs. Instead, you can reposition an existing marker by setting new coordinates.
From your code it looks like you want to get the address of the map view's center. I assume you do not want to recenter a marker each time the map has stopped moving. So, in your case it may be better to draw a fixed widget at the center of your map view, regardless of it's current coordinates - then it will not move or lag behind any events, e.g. you may get from a MapCameraObserver.
The log messages you are receiving are only warnings, so they should not have an effect on the pan gesture event you are receiving.

Flutter : Check GPS Location Mode

I have project related using maps and tracking location user , for this i used Geolocator Package. I need to check current location of user , in my flow project first i check last position of user , if last position null then i check current position of user.
Code
Position _currentPosition;
Position get currentPosition => _currentPosition;
Future<void> getCurrentPosition() async {
try {
Position lastPosition = await Geolocator().getLastKnownPosition();
if (lastPosition != null) {
print("Success Get Last Position...");
_currentPosition = lastPosition;
} else {
final currentPosition = await Geolocator().getCurrentPosition();
if (currentPosition != null) {
print("Success Get Your Current Position...");
_currentPosition = currentPosition;
} else {
throw "Can't Get Your Position";
}
}
} catch (e) {
throw e;
}
notifyListeners();
}
Then i calling getCurrentPosition Function on my button OnPressed to get location user like this :
Button GetLocation
void goToMaps() async {
final mapsProvider = context.read<MapsProvider>();
try {
print('Get Location User');
await mapsProvider.getCurrentPosition();
print('Success Get location User');
} catch (e) {
globalF.showToast(message: e.toString(), isError: true, isLongDuration: true);
}
}
First Problem is , When GPS Location Mode is Device Only I can't get LastPosition or Current Position it never print Success Get Location User and still print Get Location User.
Second Problem is , When Gps Location Mode is Battery Saving user location i got is very inaccurate, compared i used mode High Accuracy.
My question is , How can i check if gps location mode != High Accuracy , then i can show warning user to set the gps to High Accuracy.
Similiar Like this question
Thank's.
For the second problem, it happens because in High Accuracy mode android uses both GPS and network location, but in Battery Saving mode GPS module is turned off. If it was native Android, you could check provider of the location if you get location from LocationManager. But provider is not available in Flutter in Position class. You can instead use accuracy. If it is greater than a threshold and not suitable for your application, you can suggest the user to turn on High Accuracy mode.
Another options would be to use other package than Geolocator (however I'm not aware of any package that provides necessary interface) or to modify source code of Geolocatior so that it provides information about which mode is currently active.

Draw the route at runtime

I have an app with Bing Maps and I need to draw the route of the user dinamically.
Now I can only trace the user position with a pushpin. Is there some method to draw route?
UPDATE 1:
I'm using this handler assigned to geolocator.positionChanged:
private void geolocator_DrawRoute(Geolocator sender, PositionChangedEventArgs args)
{
// Need to get back onto UI thread before updating location information
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
() =>
{
//Get the current location
Location location = new Location(args.Position.Coordinate.Point.Position.Latitude,
args.Position.Coordinate.Point.Position.Longitude);
_rotta.Add(location);
if (_rotta.Count > 1)
{
var polyline = new MapPolyline { Locations = _rotta, Color = Colors.Blue, Width = 3 };
_shapeLayer.Shapes.Add(polyline);
}
//Update the position of the GPS pushpin
MapLayer.SetPosition(GpsPushpin, location);
//Update the map view to the current GPS location
MyMap.SetView(location, 18);
}));
}
UPDATE 2:
private void geolocator_DrawRoute(Geolocator sender, PositionChangedEventArgs args)
{
// Need to get back onto UI thread before updating location information
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
() =>
{
//Get the current location
var location = new Location(args.Position.Coordinate.Point.Position.Latitude, args.Position.Coordinate.Point.Position.Longitude);
_rottaLoc.Add(location);
if (_rotta == null)
{
_rotta = new MapPolyline { Locations = _rottaLoc, Color = Colors.Blue, Width = 4 };
}
else
{
_rotta.Locations = _rottaLoc;
}
_shapeLayer.Shapes.Add(_rotta);
//Update the position of the GPS pushpin
MapLayer.SetPosition(GpsPushpin, location);
//Update the map view to the current GPS location
MyMap.SetView(location, 18);
}));
}
To do this the first step would be to get the users position. If you are using JavaScript you can use the Geolocation API to get the users location and to monitor them as they move. Here is some documentation on how to do this: http://www.html5rocks.com/en/tutorials/geolocation/trip_meter/
Once you have this done you can show the users location fairly easily. If you want to draw a polyline to should their path then what you can do is the first time users position is grabbed you can create a polyline object where the first and second coordinate are this first coordinate. You can then add the polyline to the map. The next time the users location is grabbed you can simply add this new location to the polyline's array of locations.

Google maps V2 android get current location

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);
}
});

How can I get a fake 'current location' when creating an IOS Map with Xamarin Studio and the iPhone Emulator?

Problem:
When using the Xamarin iPhone emulator, the current location is not getting set on the map.
Details:
I'm trying to plot my current location on a Map, in a sample iPhone app I'm learning with Xamarin Studio and the iPhone emulator.
I have the map displayed but there's no current location getting set.
I did get asked to use my Current Location (which I'm sure I said yes/ok to) .. but it keeps centering it in San Fran, near union square :(
When ever I run my emulato, I see this text pop up:
2013-10-22 09:27:45.018 MyApp [6018:1503] MonoTouch: Socket error while connecting to MonoDevelop on 127.0.0.1:10000: Connection refused
So i'm not sure if that has something to do with it?
Ok, so lets look at some code I've got.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
map.MapType = MKMapType.Standard;
map.ShowsUserLocation = true;
map.ZoomEnabled = true;
map.ScrollEnabled = true;
map.DidUpdateUserLocation += (sender, e) => {
if (map.UserLocation != null)
{
CentreMapAtLocation(map.UserLocation.Coordinate.Latitude,
map.UserLocation.Coordinate.Longitude);
}
// User denied permission, or device doesn't have GPS/location ability.
if (!map.UserLocationVisible)
{
// TODO: Send the map somewhere or hide the map and show another message.
//CLLocationCoordinate2D coords = new CLLocationCoordinate2D(37.33233141,-122.0312186); // cupertino
//MKCoordinateSpan span = new MKCoordinateSpan(MilesToLatitudeDegrees(20), MilesToLongitudeDegrees(20, coords.Latitude));
//mapView.Region = new MKCoordinateRegion(coords, span);
}
};
private void CentreMapAtLocation(double latitude, double longitude)
{
CLLocationCoordinate2D mapCenter = new CLLocationCoordinate2D (latitude, longitude);
MKCoordinateRegion mapRegion = MKCoordinateRegion.FromDistance (mapCenter, 10000, 10000);
map.CenterCoordinate = mapCenter;
map.Region = mapRegion;
}
So it's nothing too crazy, IMO.
Anyone have any suggestions?
Have you tried setting the custom location within the simulator?
I tend to use a combination of the Custom Location setting and this tool when I need to verify locations within the iOs simulator. You won't need to make any changes to your code for this to work; it just pipes the set location into the location manager within iOs.
To my knowledge, the simulator does not support GPS or WiFi based location therefore it can't use your current location like a physical device. Perhaps someone else can clarify this.
For further information, see:
Set the location in iPhone Simulator
http://bencoding.com/2011/12/28/setting-you-location-in-the-ios-5-0-simulator/