Flutter + Google Maps: How to animate the camera to target location - google-maps

i was wondering if you could give me an example on "animating" the camera of a map to a target location based on latitude and longitude.
Sorry I'm just very new to flutter.
Thanks in advance.

Take a look at the official google_maps_flutter page. It has exactly the same example you are looking for.
Basically, you can control the camera with the GoogleMapController object after initializing it with the onMapCreated callback of the GoogleMap widget.
Future<void> _goToTheLake() async {
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(_kLake));
}

Related

How to make marker in flutter google map move with camera and not with map

i watched a video in which the location marker moved with the camera and was not stuck to the map. I am not sure if this is the official google map for flutter, but if it is how was this achieved.
i tried passing the controllers camera position to the marker, but it didnt work, as i am not sure the markers position got updated with the camera movement.
mapController.addMarker(MarkerOptions(
icon: BitmapDescriptor.fromAsset("assets/images/marker.png"),
position: LatLng(mapController.cameraPosition.target.latitude,mapController.cameraPosition.target.longitude),
infoWindowText: InfoWindowText("Pick up location", "Pick up location")
));
You can do that by creating a new marker every time the camera moves but providing the same marker id. It won't, actually, create a new marker but it will start moving the same marker according to the camera position.
Here's how you can do it:
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class YourMapView extends StatefulWidget {
#override
_YourMapViewState createState() => _YourMapViewState();
}
class _YourMapViewState extends State<YourMapView> {
final markers = Set<Marker>();
MarkerId markerId = MarkerId("YOUR-MARKER-ID");
LatLng latLng = LatLng(43.2994, 74.2179);
#override
void initState() {
markers.add(
Marker(
markerId: markerId,
position: latLng,
),
);
super.initState();
}
#override
Widget build(BuildContext context) {
return GoogleMap(
initialCameraPosition: CameraPosition(target: latLng,zoom: 14.34),
markers: markers,
onCameraMove: (position){
setState(() {
markers.add(Marker(markerId: markerId,position: position.target));
});
},
);
}
}
you can do it using these properties.
onCameraMove: ,
onCameraIdle: ,
onCameraMoveStarted:
,
If you want the marker just for visual purposes, you can place a "fake" marker on top of the map that is always centered and get the camera position to get the actual camera coordinates.
the other way is to update the marker onCameraMove, the marker will not perfeclty be at the center everytime and it will jerk.

How to integrate with google maps in flutter?

I started to develop with flutter and I want to use google_maps_flutter package.
I was going through medium post in order to add map to my app (https://medium.com/flutter-io/google-maps-and-flutter-cfb330f9a245).
I added this code in AppDelegate.m
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import "GoogleMaps/GoogleMaps.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:#"******"];
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
#end
and this to Info.plist
<key>io.flutter.embedded_views_preview</key>
<true/>
and this is the widget code:
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
GoogleMapController mapController;
final LatLng _center = const LatLng(45.521563, -122.677433);
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Maps Sample App'),
backgroundColor: Colors.green[700],
),
body: GoogleMap(
onMapCreated: _onMapCreated,
options: GoogleMapOptions(
cameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
),
),
),
);
}
}
As the example in the post, but unfortunately I get a screen with the google maps widget but not showing a map. like this: https://i.imgur.com/HGam5ac.jpg (I can't upload an image because low reputation, sorry :( ).
Is there a problem with the sdk? or there is something to change in the code?
Thank you for your help!!
I faced the same issue and spent some time figuring it out.
Please note it is not advisable to unrestrict your API key and should be a temporary solution.
First of all, you cannot use the Google Maps APIs if you have not enabled billing in your Google Cloud Platform account.
I fixed this issue by making sure my API Key was not restricted.
I deleted my previous API key and created a new API key but this time I did not restrict the API key.
The map loaded correctly afterwards.
If you intend to use the geolocation services, it might help to add to your ios/Runner/Info.plist file the code below
<key>NSLocationWhenInUseUsageDescription</key>
<string>Reason for requesting location permission</string>
If you do not know how to create the API keys follow this link Get API Key.
This is how my API key configuration looks like
And this is how my maps page in my app looks like

Are user uploaded Street Views available in the Google Maps Android API v2?

Let's use this location as an example. The lat/lng are 39.9477959/-75.1850599.
I can use the Google JS API to pull up this Street View no problem. However, using these same coordinates and Google Maps Android API, all I get is a black screen.
To reproduce the issue clone and change the coordinates here.
I think it will have no conflict if you are using the same coordinates. Based from this related SO question, black screen appears when location is not present. Just check whether it was loaded or not.
#Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
mPanorama.setOnStreetViewPanoramaChangeListener(new StreetViewPanorama.OnStreetViewPanoramaChangeListener() {
#Override
public void onStreetViewPanoramaChange(StreetViewPanoramaLocation streetViewPanoramaLocation) {
if (streetViewPanoramaLocation != null && streetViewPanoramaLocation.links != null) {
// location is present
} else {
// location not available
}
}
});
Here is an example from Google documentation:
public class StreetViewPanoramaBasicDemoActivity extends AppCompatActivity {
// George St, Sydney
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.street_view_panorama_basic_demo);
SupportStreetViewPanoramaFragment streetViewPanoramaFragment =
(SupportStreetViewPanoramaFragment)
getSupportFragmentManager().findFragmentById(R.id.streetviewpanorama);
streetViewPanoramaFragment.getStreetViewPanoramaAsync(
new OnStreetViewPanoramaReadyCallback() {
#Override
public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
// Only set the panorama to SYDNEY on startup (when no panoramas have been
// loaded which is when the savedInstanceState is null).
if (savedInstanceState == null) {
panorama.setPosition(SYDNEY);
}
}
});
}
}
Hope this helps!
It seems not, see Issue 7033. There's a new, better workaround there.

Marker mouseover state in JxMaps

Is there a way to add mouseover state to a marker in Google Maps embedded in Java app via JxMaps? I was looking for a way to show whether the marker is clickable or not on hover. However, I didn’t find a suitable method that sets this icon the API
To implement functionality like this, you have to attach to marker mouse events and change marker icon (using Marker.setIcon method) depending on mouse position.
marker.addEventListener("mouseover", new MapEvent() {
#Override
public void onEvent() {
marker.setIcon(hoverIcon);
}
}});
marker.addEventListener("mouseout", new MapEvent() {
#Override
public void onEvent() {
marker.setIcon(normalIcon);
}
}});

How to disable streetview using gwt?

There is a "person" icon which allows user to view "Street View". I don't want this functionality on my map, is there a way to remove it or disable it? And I am not using JavaScript I am doing it using GWT.
Google-Maps v3 am using.
private void initMap() {
mapVp.setSize(String.valueOf(BodyPanel.bodyWidth - 505),
String.valueOf(BodyPanel.bodyHeight + 5));
LatLng myLatLng = LatLng.create(24.440099, 46.843498);
final MapOptions myOptions = MapOptions.create();
myOptions.setZoom(5);
myOptions.setCenter(myLatLng);
setStreetViewControl(myOptions.getJso(), false); //Added now
myOptions.setMapTypeId(MapTypeId.ROADMAP);
myOptions.setMapTypeControl(true);
Timer load = new Timer() {
#Override
public void run() {
map = GoogleMap.create(mapVp.getElement(), myOptions);
hPanel.add(LoginDashboardModule
.mapLegends(map, "Live Tracking"));
}
};
load.schedule(1000);
}
This is my code
I am getting error after adding this line setStreetViewControl(myOptions.getJso(), false);
I just used the line below and got the output.
myOptions.setStreetViewControl(false);
You can do this with JSNI.
public static MapWidget createMapWidget(double lat, double lng,
final String width, final String height, int zoomLevel) {
// Set the options of the map widget.
MapOptions options = new MapOptions();
options.setZoom(zoomLevel);
options.setMapTypeId(new MapTypeId().getRoadmap());
setStreetViewControl(options.getJso(), false);
options.setCenter(new LatLng(lat, lng));
/* The map widget */
final MapWidget mapWidget = new MapWidget(options);
mapWidget.setWidth(width);
mapWidget.setHeight(height);
return mapWidget;
}
public static native void setStreetViewControl(JavaScriptObject obj,
boolean control) /*-{
if (obj)
obj.streetViewControl = control;
}-*/;
You can do so in the JavaScript API by setting streetViewControl to false. So I assume the GWT wrapper does provide a similar option.