flutter_streetview_web 1.0.1
flutter_streetview_web: ^1.0.1 copied to clipboard
A Flutter package for Google Street View integration with customizable markers.
πΊοΈ Flutter Streetview Web #
A Flutter widget for integrating Google Street View using WebView. Leverages the Google Maps JavaScript API to provide a seamless Street View experience within your Flutter app.
π Features
- πΊ Display Google Street View within Flutter
- π Add custom markers (PNG & SVG)
- π¨ Customizable loading and error screens (
loadingBuilder,errorBuilder) - Customizable StreetView controls
- Flutter βοΈ JavaScript communication
- Comprehensive error handling
π Prerequisites
Google Maps API Key: Obtain from Google Cloud Console. Enable Required APIs: Maps JavaScript API, Street View Static API.
π Getting Google Maps API Key #
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable these APIs:
- Maps JavaScript API
- Create credentials (API Key)
- Set application restrictions (recommended)
- Copy your API key
Important: Add your domain to HTTP referrers in API key restrictions.
π― Basic Usage
import 'package:flutter_streetview_web/flutter_street_view.dart';
FlutterStreetView(
apiKey: "YOUR_GOOGLE_MAPS_API_KEY",
latitude: 41.107490, // Istanbul/Beykoz coordinates
longitude: 29.078115,
)
π Resizable Street View You can wrap FlutterStreetView inside a SizedBox (or any container) to control its width and height:
SizedBox(
width: 300,
height: 200,
child: FlutterStreetView(
apiKey: "YOUR_GOOGLE_MAPS_API_KEY",
latitude: 41.107490,
longitude: 29.078115,
),
)
β This allows embedding Street View in a smaller portion of your UI instead of occupying the full screen.
π Adding Markers
Flutter Streetview Web - Marker Customization #
You can customize markers in Street View using MarkerOptions and MarkerIconType.
MarkerIconType #
Defines the type of marker icon you want to display in Street View:
- defaultIcon β Uses the default Google Street View marker. No additional parameters are needed.
- png β Uses a PNG image from
customIconPath. - svg β Uses an SVG image from
customIconPath. Note: iconType is automatically determined based on the parameters you provide: customIconPathends with.pngβ png π’customIconPathends with.svgβ svg π΅- If no
customIconPathβ defaultIcon βͺ
MarkerOptions #
Options to customize a marker in Street View:
-
markerLat (
double?) β Latitude of the marker. Required for displaying a marker. -
markerLng (
double?) β Longitude of the marker. Required for displaying a marker. -
iconType (
MarkerIconType) β Type of the marker icon. Automatically set depending oncustomIconPath. Default:defaultIcon. -
customIconPath (
String?) β Path to your custom PNG or SVG asset. -
scaledWidth (
double?) β Width of the marker icon in pixels. Default:40. -
scaledHeight (
double?) β Height of the marker icon in pixels. Default:40. -
anchorX (
double?) β X coordinate of the anchor point. Default:0. -
anchorY (
double?) β Y coordinate of the anchor point. Default:50.
Example Usage #
Default marker:
FlutterStreetView(
apiKey: "YOUR_KEY",
latitude: 41.107490,
longitude: 29.078115,
markerOptions: MarkerOptions(
markerLat: 41.0082,
markerLng: 28.9784,
iconType: MarkerIconType.defaultIcon, // Optional
),
)
Custom PNG marker:
FlutterStreetView(
apiKey: "YOUR_KEY",
latitude: 41.107490,
longitude: 29.078115,
markerOptions: MarkerOptions(
markerLat: 41.0082,
markerLng: 28.9784,
iconType: MarkerIconType.png,
customIconPath: "assets/icons/location_pin.png",
scaledWidth: 48,
scaledHeight: 48,
anchorX: 0,
anchorY: 50,
),
)
Custom SVG marker:
FlutterStreetView(
apiKey: "YOUR_KEY",
latitude: 41.107490,
longitude: 29.078115,
markerOptions: MarkerOptions(
markerLat: 41.0082,
markerLng: 28.9784,
iconType: MarkerIconType.svg,
customIconPath: "assets/icons/location_pin.svg",
scaledWidth: 48,
scaledHeight: 48,
),
)
βοΈ StreetView Options
FlutterStreetView(
apiKey: "YOUR_KEY",
latitude: 41.107490,
longitude: 29.078115,
streetViewOptions: const StreetViewOptions(
panControl: true,
zoomControl: true,
linksControl: true,
fullscreenControl: true,
motionTrackingControl: true,
addressControl: true,
showRoadLabels: true,
clickToGo: true,
scrollwheel: true,
disableDefaultUI: false,
povHeading: 0,
povPitch: 0,
povZoom: 0,
),
)
π¨ Customization
Custom loading indicator:
FlutterStreetView(
apiKey: "YOUR_KEY",
latitude: 41.107490,
longitude: 29.078115,
loadingBuilder: (context) => Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
),
),
)
Custom error screen:
FlutterStreetView(
apiKey: "YOUR_KEY",
latitude: 41.107490,
longitude: 29.078115,
errorBuilder: (context, errorMessage) => Center(
child: Container(
padding: EdgeInsets.all(16),
color: Colors.red[100],
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.error_outline, color: Colors.red, size: 48),
SizedBox(height: 16),
Text(
errorMessage,
textAlign: TextAlign.center,
style: TextStyle(color: Colors.red[800]),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () => _controller.reload(),
child: Text('Retry'),
),
],
),
),
),
)
π‘ Event Handling
Handle messages from JavaScript:
FlutterStreetView(
apiKey: "YOUR_KEY",
latitude: 41.107490,
longitude: 29.078115,
onMessageReceived: (message) {
print('Received message from Street View: $message');
},
)
Error handling:
FlutterStreetView(
apiKey: "YOUR_KEY",
latitude: 41.107490,
longitude: 29.078115,
onError: (error) {
print('Street View Error: ${error.errorType} - ${error.message}');
switch (error.errorType) {
case StreetViewErrorType.apiKeyMissing:
break;
case StreetViewErrorType.locationUnavailable:
break;
}
},
)
π¨ Error Types
- β οΈ API Key Missing: Make sure your Google Maps API Key is valid!
- β οΈ Location Unavailable: Street View is not available for the specified location.
- β οΈ Web View Error: WebView encountered an error.
- β οΈ Asset Load Error: Failed to load custom marker asset.
- β οΈ Initialization Failed: Failed to initialize Street View.
π Programmatic Control
final WebViewController _controller;
_controller.reload();
_controller.runJavaScript('changeLocation(40.7128, -74.0060);');
_controller.setNavigationDelegate(NavigationDelegate(
onNavigationRequest: (request) {
return NavigationDecision.navigate;
},
));
π§ Troubleshooting #
Common Issues #
Blank Screen
- Check API key validity
- Verify internet connection
- Check console for JavaScript errors
Marker Not Showing
- Verify asset paths are correct
- Check marker coordinates are within view
Slow Performance
- Optimize marker images
- Reduce WebView complexity
Debug Mode #
Enable debug logging:
FlutterStreetView(
onMessageReceived: (message) {
debugPrint('StreetView: $message');
},
)
π Notes
- Internet Connection: Requires active internet connection for Google Maps API
- API Key Restrictions: Configure API key restrictions in Google Cloud Console
- Platform Support: Works on iOS and Android
- Performance: For best performance, use compressed images for custom markers
- Caching: WebView content is cached for better performance
π License
MIT License
π Acknowledgments
Google Maps JavaScript API team, Flutter team, webview_flutter package maintainers. Enjoy exploring the world with Street View in your Flutter app! π