// ============================================ // db/gps_links.go // Génération de liens GPS vers différentes plateformes // ============================================ package db import ( "fmt" "net/url" ) // MapLinks contient les liens vers différentes plateformes de cartographie type MapLinks struct { GoogleMaps string `json:"google_maps"` GoogleMapsApp string `json:"google_maps_app"` Waze string `json:"waze"` WazeApp string `json:"waze_app"` AppleMaps string `json:"apple_maps"` OpenStreetMap string `json:"openstreetmap"` BingMaps string `json:"bing_maps"` HereMaps string `json:"here_maps"` } // GenerateMapLinks génère tous les liens de cartes pour une position GPS func (d *Database) GenerateMapLinks(lat, lon float64, label string) MapLinks { // Encoder le label pour l'URL encodedLabel := url.QueryEscape(label) return MapLinks{ // Google Maps (Web) GoogleMaps: fmt.Sprintf( "https://www.google.com/maps?q=%.6f,%.6f&label=%s", lat, lon, encodedLabel, ), // Google Maps (App - Deep link) GoogleMapsApp: fmt.Sprintf( "https://maps.google.com/?q=%.6f,%.6f", lat, lon, ), // Waze (Web) Waze: fmt.Sprintf( "https://www.waze.com/ul?ll=%.6f,%.6f&navigate=yes", lat, lon, ), // Waze (App - Deep link) WazeApp: fmt.Sprintf( "waze://?ll=%.6f,%.6f&navigate=yes", lat, lon, ), // Apple Maps (iOS/macOS) AppleMaps: fmt.Sprintf( "http://maps.apple.com/?ll=%.6f,%.6f&q=%s", lat, lon, encodedLabel, ), // OpenStreetMap OpenStreetMap: fmt.Sprintf( "https://www.openstreetmap.org/?mlat=%.6f&mlon=%.6f#map=16/%.6f/%.6f", lat, lon, lat, lon, ), // Bing Maps BingMaps: fmt.Sprintf( "https://www.bing.com/maps?cp=%.6f~%.6f&lvl=16", lat, lon, ), // HERE Maps HereMaps: fmt.Sprintf( "https://wego.here.com/?map=%.6f,%.6f,16,normal", lat, lon, ), } } // GenerateNavigationLink génère un lien de navigation depuis une origine vers une destination func (d *Database) GenerateNavigationLink(fromLat, fromLon, toLat, toLon float64, platform string) string { switch platform { case "google": return fmt.Sprintf( "https://www.google.com/maps/dir/?api=1&origin=%.6f,%.6f&destination=%.6f,%.6f&travelmode=driving", fromLat, fromLon, toLat, toLon, ) case "waze": return fmt.Sprintf( "https://www.waze.com/ul?ll=%.6f,%.6f&navigate=yes&from=%.6f,%.6f", toLat, toLon, fromLat, fromLon, ) case "apple": return fmt.Sprintf( "http://maps.apple.com/?saddr=%.6f,%.6f&daddr=%.6f,%.6f", fromLat, fromLon, toLat, toLon, ) default: return fmt.Sprintf( "https://www.google.com/maps/dir/?api=1&origin=%.6f,%.6f&destination=%.6f,%.6f", fromLat, fromLon, toLat, toLon, ) } } // GenerateMapLinksForCommand génère les liens de navigation pour une commande // (depuis la position du livreur vers la destination de la commande) func (d *Database) GenerateMapLinksForCommand(commandID int, deliverymanUsername string) (map[string]string, error) { // 1. Récupérer la position du livreur livreurLat, livreurLon, err := d.GetDeliveryPersonLocation(deliverymanUsername) if err != nil { return nil, fmt.Errorf("position livreur non disponible: %w", err) } // 2. Récupérer la destination de la commande command, err := d.GetCommandByID(commandID) if err != nil { return nil, fmt.Errorf("commande non trouvée: %w", err) } var destLat, destLon float64 if command["dest_latitude"] != nil { if latVal, ok := command["dest_latitude"].(float64); ok { destLat = latVal } } if command["dest_longitude"] != nil { if lonVal, ok := command["dest_longitude"].(float64); ok { destLon = lonVal } } if destLat == 0 && destLon == 0 { return nil, fmt.Errorf("coordonnées destination invalides") } // 3. Générer les liens de navigation return map[string]string{ "google_maps": d.GenerateNavigationLink(livreurLat, livreurLon, destLat, destLon, "google"), "waze": d.GenerateNavigationLink(livreurLat, livreurLon, destLat, destLon, "waze"), "apple_maps": d.GenerateNavigationLink(livreurLat, livreurLon, destLat, destLon, "apple"), }, nil }