Commit 663799e9 authored by shigemi miura's avatar shigemi miura

サーバー側で描画したラインデータを表示

parent cbac0b93
......@@ -65,6 +65,7 @@
A0F472F6BC78C5F1C5471836 /* Pods_SailAssistTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0D252BA721FD314FAE4C2B4C /* Pods_SailAssistTests.framework */; };
D50533FA2EA7956900975ADD /* TransparentBackgroundViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = D50533F92EA7955500975ADD /* TransparentBackgroundViewModifier.swift */; };
D50533FC2EA8A93D00975ADD /* TextEditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D50533FB2EA8A93A00975ADD /* TextEditorView.swift */; };
D50533FE2EA9A9BA00975ADD /* ConvertGeoJSONS.swift in Sources */ = {isa = PBXBuildFile; fileRef = D50533FD2EA9A9A200975ADD /* ConvertGeoJSONS.swift */; };
D5056D3E2E8F4DE20076AC88 /* SafariView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5056D3D2E8F4DD80076AC88 /* SafariView.swift */; };
D5056D482E925FE90076AC88 /* LayoutConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5056D472E925FDF0076AC88 /* LayoutConstants.swift */; };
D5056D522E9343300076AC88 /* TaskTitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5056D512E9343270076AC88 /* TaskTitleView.swift */; };
......@@ -259,6 +260,7 @@
CA31D32D54D7C200EF057679 /* Pods-SailAssistTests.canary.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SailAssistTests.canary.xcconfig"; path = "Target Support Files/Pods-SailAssistTests/Pods-SailAssistTests.canary.xcconfig"; sourceTree = "<group>"; };
D50533F92EA7955500975ADD /* TransparentBackgroundViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransparentBackgroundViewModifier.swift; sourceTree = "<group>"; };
D50533FB2EA8A93A00975ADD /* TextEditorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextEditorView.swift; sourceTree = "<group>"; };
D50533FD2EA9A9A200975ADD /* ConvertGeoJSONS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConvertGeoJSONS.swift; sourceTree = "<group>"; };
D5056D3D2E8F4DD80076AC88 /* SafariView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafariView.swift; sourceTree = "<group>"; };
D5056D472E925FDF0076AC88 /* LayoutConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutConstants.swift; sourceTree = "<group>"; };
D5056D512E9343270076AC88 /* TaskTitleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskTitleView.swift; sourceTree = "<group>"; };
......@@ -639,6 +641,7 @@
022789122AE602D800A87787 /* ServerSession */ = {
isa = PBXGroup;
children = (
D50533FD2EA9A9A200975ADD /* ConvertGeoJSONS.swift */,
D58E1F532B142A510092D8FE /* SessionEcaArea.swift */,
D5AE351C2AEBA6FC00059889 /* SessionLogin.swift */,
02CD06922AE88970005F8D8F /* ServerSession.swift */,
......@@ -1139,6 +1142,7 @@
020B98632ADD14E50029DE4C /* ChatView.swift in Sources */,
D55186132E7E9158004CD8BD /* ImageQualityPreset.swift in Sources */,
02CE4DDA2ADFBA72002E79BC /* MapRepresentable.swift in Sources */,
D50533FE2EA9A9BA00975ADD /* ConvertGeoJSONS.swift in Sources */,
D5AF8A542E892D0500BECA22 /* FuelSwitchingMainView.swift in Sources */,
020B985E2ADCFF130029DE4C /* PreferencesKey.swift in Sources */,
0227890C2AE22E0B00A87787 /* SharingData.swift in Sources */,
......
......@@ -33,7 +33,8 @@ struct MyChatContentView: View {
isSafariPresented = true
}
} else {
Text(msg)
let chatMsg = message.type == 4 ? "[Map Drawing]" : msg
Text(chatMsg)
.font(FontStyle.DefaultText.font)
.foregroundColor(ColorSet.BodyChat.color)
.padding(15)
......
......@@ -33,7 +33,8 @@ struct OtherChatContentView: View {
isSafariPresented = true
}
} else {
Text(msg)
let chatMsg = message.type == 4 ? "[Map Drawing]" : msg
Text(chatMsg)
.font(FontStyle.DefaultText.font)
.foregroundColor(ColorSet.BodyChat.color)
.padding(15)
......
......@@ -21,7 +21,7 @@ struct ResGetMessages: Codable {
struct ChatMessage: Codable {
var shipId: Int
var messageId: String //各メッセージ固有ID
var type: Int //0:テキスト , 1:スタンプ , 2:画像
var type: Int //0:テキスト , 1:スタンプ , 2:画像, 4:Map Drawing, 5:Map Drawing Erase
var time: String //投稿日時
var location: Int //1:Shore , 2:Ship
var from: String? //投稿者名
......
......@@ -16,6 +16,7 @@ struct MapRepresentable: UIViewControllerRepresentable {
@ObservedObject var location = SharingData.location
@ObservedObject var map = SharingData.map
@ObservedObject var pushHistory = SharingData.pushHistory
@ObservedObject var message = SharingData.message
@State var mapVC = MapViewController()
//作成したいViewControllerを返すメソッド
......@@ -127,6 +128,7 @@ class MapViewController : UIViewController {
self.addLayers()
self.latitudeLongitudeLinesLayer()
self.latitudeLongitudeLabelsLayer()
self.messageLinesLayer()
mapView.mapboxMap.onEvery(event: .cameraChanged) { _ in
self.updateOnViewportOrZoomChange()
......@@ -262,6 +264,8 @@ class MapViewController : UIViewController {
case AlertAreaSymbol
case AlertAreaLine
case AlertAreaFill
case MessageSymbol
case MessageLines
case LatitudeLongitudeLines
case LatitudeLongitudeLabels
}
......@@ -282,6 +286,8 @@ class MapViewController : UIViewController {
var alertAreaFill = MapSource(layer: .AlertAreaFill)
var alertAreaLine = MapSource(layer: .AlertAreaLine)
var alertAreaSymbol = MapSource(layer: .AlertAreaSymbol)
var messageSymbol = MapSource(layer: .MessageSymbol)
var messageLines = MapSource(layer: .MessageLines)
var latitudeLongitudeLines = MapSource(layer: .LatitudeLongitudeLines)
var latitudeLongitudeLabels = MapSource(layer: .LatitudeLongitudeLabels)
......@@ -326,6 +332,7 @@ class MapViewController : UIViewController {
self.latitudeLongitudeLinesLayer()
self.latitudeLongitudeLabelsLayer()
self.messageLinesLayer()
}
//MARK: - 地図の縮尺による間隔
......@@ -392,7 +399,7 @@ class MapViewController : UIViewController {
// メインスレッドで地図に反映
DispatchQueue.main.async {
print(debug: "geoJSON:\(geoJSON)")
// print(debug: "geoJSON:\(geoJSON)")
let sourceId = self.latitudeLongitudeLines.sourceId
// 既存のソースがある場合は更新
......@@ -588,6 +595,54 @@ class MapViewController : UIViewController {
return FeatureCollection(features: features)
}
//MARK: - メッセージライン
private func messageLinesLayer() {
let visibleRegion = mapView.mapboxMap.coordinateBounds(for: mapView.frame)
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
guard let self = self else { return }
let mapMessages = SharingData.message.messages.filter { $0.type == 4 }
var features: [Feature] = []
for message in mapMessages {
if let messageJson = message.message,
let geoJSON = convertToGeoJSON(
minLatitude: visibleRegion.southwest.latitude,
maxLatitude: visibleRegion.northeast.latitude,
minLongitude: visibleRegion.southwest.longitude,
maxLongitude: visibleRegion.northeast.longitude,
from: messageJson
)
{
features.append(contentsOf: geoJSON.features)
}
}
if features.isEmpty { return }
let featureCollection = FeatureCollection(features: features)
DispatchQueue.main.async {
let sourceId = self.messageLines.sourceId
// 既存のソースがある場合は更新
if self.mapView.mapboxMap.style.sourceExists(withId: sourceId) {
try? self.mapView.mapboxMap.style.updateGeoJSONSource(withId: sourceId, geoJSON: .featureCollection(featureCollection))
} else {
// 初回のみソースを追加
self.messageLines.source.data = .featureCollection(featureCollection)
try? self.mapView.mapboxMap.style.addSource(self.messageLines.source, id: sourceId)
var lineLayer = LineLayer(id: self.messageLines.layerId)
lineLayer.source = sourceId
lineLayer.lineColor = .expression(Exp(.get) {PropertyKey.Color.rawValue})
lineLayer.lineWidth = .expression(Exp(.get) {PropertyKey.LineWidth.rawValue})
try? self.mapView.mapboxMap.style.addLayer(lineLayer)
}
}
}
}
/**
* 自船シンボル表示レイヤー
*/
......
import Foundation
import MapboxMaps
//import SwiftUI
//import UIKit
struct LineData: Codable {
var lines: [Line]
}
struct Line: Codable {
var color: String
var thickness: Int
var points: [PointInf]
}
struct PointInf: Codable {
var lat: Double
var lon: Double
}
//MARK: - 色変換
func hexStringToRGBA(_ hex: String, alpha: CGFloat = 1.0) -> String? {
var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")
var rgb: UInt64 = 0
guard Scanner(string: hexSanitized).scanHexInt64(&rgb) else { return nil }
let red = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
let green = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
let blue = CGFloat(rgb & 0x0000FF) / 255.0
return "rgba(\(Int(red * 255)), \(Int(green * 255)), \(Int(blue * 255)), \(alpha))"
}
//MARK: - JSONフォーマットをGeoJSONに変換する関数
func convertToGeoJSON(
minLatitude: Double,
maxLatitude: Double,
minLongitude: Double,
maxLongitude: Double,
from jsonString: String
) -> FeatureCollection? {
let decoder = JSONDecoder()
do {
let lineData = try decoder.decode(LineData.self, from: Data(jsonString.utf8))
let features = lineData.lines.compactMap { line -> Feature? in
let filteredCoordinates = line.points.compactMap { point -> CLLocationCoordinate2D? in
let latitude = point.lat
let longitude = point.lon
// 座標が表示範囲内に含まれるかを確認
if latitude >= minLatitude, latitude <= maxLatitude,
longitude >= minLongitude, longitude <= maxLongitude {
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
} else {
return nil
}
}
guard !filteredCoordinates.isEmpty else { return nil }
let lineString = LineString(filteredCoordinates)
var feature = Feature(geometry: .lineString(lineString))
feature.properties = [
MapViewController.PropertyKey.Color.rawValue: .string(hexStringToRGBA(line.color) ?? "rgba(0, 0, 0, 1.0)"),
MapViewController.PropertyKey.LineWidth.rawValue: .number(Double(line.thickness))
]
return feature
}
guard !features.isEmpty else { return nil }
return FeatureCollection(features: features)
} catch {
print("エラー: \(error)")
return nil
}
}
//MARK: - GeoJSONを元の形式に逆変換する関数
//func convertToOriginalFormat(from geoJSONString: String) -> String? {
// let decoder = JSONDecoder()
// let encoder = JSONEncoder()
// encoder.outputFormatting = .prettyPrinted
//
// do {
// // GeoJSON文字列をデコード
// let geoJSON = try decoder.decode(GeoJSON.self, from: Data(geoJSONString.utf8))
//
// // 元の形式の構造体を作成
// let lines = geoJSON.features.map { feature in
// Line(
// color: feature.properties.color,
// thickness: feature.properties.thickness,
// points: feature.geometry.coordinates.map { PointInf(lat: $0[1], lon: $0[0]) }
// )
// }
// let lineData = LineData(lines: lines)
//
// // 元の形式をエンコード
// let originalFormatData = try encoder.encode(lineData)
// return String(data: originalFormatData, encoding: .utf8)
//
// } catch {
// print("エラー: \(error)")
// return nil
// }
//}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment