GeoPoint และช่วง (GeoRectBounds)
MapConductor มีประเภทพิกัดเพียงประเภทเดียวคือ GeoPoint คุณไม่จำเป็นต้องยุ่งเกี่ยวกับ LatLng, CLLocationCoordinate2D หรือ LngLat เฉพาะของผู้ให้บริการ ช่วงสี่เหลี่ยมจัดการโดย GeoRectBounds และแสดงด้วยสองจุด: ตะวันตกเฉียงใต้และตะวันออกเฉียงเหนือ ทั้งสองมีโครงสร้างสมาชิกเหมือนกันบนทั้ง 3 แพลตฟอร์ม
01 · GeoPoint
ค่าที่ไม่เปลี่ยนแปลงที่มีสามส่วน: ละติจูด ลองจิจูด และความสูง คุณสามารถละเว้นความสูงได้ และในกรณีนั้นจะถือว่าเป็น 0 การเปรียบเทียบความเท่ากันและแฮชจะถูกกำหนดโดยเนื้อหาของละติจูดและลองจิจูด (และความสูง) ดังนั้นคุณสามารถใช้ค่านี้โดยตรงเป็นคีย์ในแผนที่หรือสำหรับการตรวจสอบความแตกต่างได้
มีสามวิธีในการสร้าง: ตัวสร้าง, โรงงานที่ระบุลำดับของละติจูดและลองจิจูดอย่างชัดเจน และการแปลงจากประเภทพิกัดอื่น สำหรับลำดับลองจิจูด,ละติจูดซึ่งสับสนง่าย ได้มีการตั้งชื่อเพื่อให้แยกแยะได้
val tokyo = GeoPoint(35.6812, 139.7671) val haneda = GeoPoint.fromLatLong(35.548852, 139.784086) val fromLngLat = GeoPoint.fromLongLat(139.7671, 35.6812) // จากค่าใด ๆ ที่ implements GeoPointInterface val copied = GeoPoint.from(anyPositionLike) val url = tokyo.toUrlValue() // "35.681200,139.767100"
let tokyo = GeoPoint(latitude: 35.6812, longitude: 139.7671) let haneda = GeoPoint.fromLatLong(latitude: 35.548852, longitude: 139.784086) let fromLngLat = GeoPoint.fromLongLat(longitude: 139.7671, latitude: 35.6812) // จากค่าใด ๆ ที่สอดคล้องกับ GeoPointProtocol let copied = GeoPoint.from(position: anyPositionLike) let url = tokyo.toUrlValue() // "35.681200,139.767100"
import { createGeoPoint, fromLatLng, fromLngLat, GeoPoint } from '@mapconductor/js-sdk-core';
const tokyo = createGeoPoint({ latitude: 35.6812, longitude: 139.7671 });
const haneda = fromLatLng({ latitude: 35.548852, longitude: 139.784086 });
const fromLngLatPoint = fromLngLat({ longitude: 139.7671, latitude: 35.6812 });
// เรียกสิ่งเดียวกันจากเนมสเปซก็ได้
const same = GeoPoint.fromLatLong({ latitude: 35.6812, longitude: 139.7671 });
const url = tokyo.toUrlValue(6); // "35.681200,139.767100"fromLatLng และ fromLatLong รวมถึง fromLngLat และ fromLongLat เป็นนามแฝงของสิ่งเดียวกัน คุณสามารถเลือกใช้สิ่งที่เหมาะสมกับการตั้งชื่อโค้ดที่มีอยู่ของคุณได้
02 · การทำให้เป็นมาตรฐานและความถูกต้อง
พิกัดจากข้อมูลภายนอกอาจอยู่นอกช่วง หรือมีลองจิจูดมากกว่า 360 องศา มี normalize สำหรับปรับให้อยู่ในช่วง และ isValid สำหรับตรวจสอบว่าอยู่ในช่วงหรือไม่
จัดให้อยู่ในช่วง
ค่าละติจูดถูกตัดทอนให้อยู่ในช่วง −90~90 และลองจิจูดถูกรักษาระดับให้ −180~180 ลองจิจูด 200 จะกลายเป็น −160
ตรวจสอบว่าอยู่ในช่วงหรือไม่
true เมื่อละติจูดอยู่ในช่วง −90~90 และลองจิจูดอยู่ในช่วง −180~180 เท่านั้น ใช้สำหรับการตรวจสอบเวลานำเข้า
// ให้มาในรูปฟังก์ชันส่วนขยายของ GeoPointInterface val safe = raw.normalize() if (!raw.isValid()) return
let safe = raw.normalize()
guard raw.isValid() else { return }const safe = raw.normalize(); if (!raw.isValid()) return;
03 · GeoRectBounds
แสดงสี่เหลี่ยมผืนผ้าด้วยสองจุด: ตะวันตกเฉียงใต้และตะวันออกเฉียงเหนือ การใช้งานหลักคือเป็น "กล่องที่เติบโต" เมื่อเพิ่มจุด ช่วงที่ครอบคลุมทั้งหมดจะขยายออก เมื่อสร้างขึ้นใหม่จะว่างเปล่า (isEmpty) และ southWest กับ northEast จะยังไม่ได้ตั้งค่า
// สร้างขอบเขตที่ครอบคลุมเส้นทางทั้งหมด แล้วปรับกล้องให้พอดี
val bounds = GeoRectBounds()
routePoints.forEach { bounds.extend(it) }
mapViewState.fitBounds(bounds = bounds, padding = 48)
// ดึงข้อมูลกว้างกว่าพื้นที่ที่มองเห็นเล็กน้อย
val fetchArea = bounds.expandedByDegrees(latPad = 0.05, lonPad = 0.05)let bounds = GeoRectBounds()
routePoints.forEach { bounds.extend(point: $0) }
mapState.fitBounds(bounds: bounds, padding: 48)
let fetchArea = bounds.expandedByDegrees(latPad: 0.05, lonPad: 0.05)import { createGeoRectBounds } from '@mapconductor/js-sdk-core';
const bounds = createGeoRectBounds();
routePoints.forEach(p => bounds.extend(p));
mapViewState.fitBounds(bounds, 48);
const fetchArea = bounds.expandedByDegrees(0.05, 0.05);04 · จุดที่ควรใช้ช่วง
GeoRectBounds ปรากฏในหลายส่วนของ API เนื่องจากเป็นประเภทเดียวกันหมด คุณสามารถนำช่วงที่สร้างขึ้นมาใช้ซ้ำได้
ซูมกล้องให้เห็นทั้งช่วง padding คือระยะขอบหน้าจอ (พิกเซลเชิงตรรกะ)
เมื่อวางภาพลงบนแผนที่ ให้ระบุตำแหน่งด้วยช่วง
กำหนดช่วงที่กล้องไม่สามารถออกไปได้เป็นสี่เหลี่ยม (Android · React)
MapCameraPosition ที่ได้รับใน onCameraMove มีช่วงที่แสดงอยู่ สามารถดึงข้อมูลเฉพาะของช่วงนั้นได้
รุ่นที่ขยายเป็นหน่วยเมตร ดูหน้าการคำนวณทางภูมิศาสตร์
ใช้ intersects เพื่อตัดสินใจการสร้างและการดึงไทล์ซ้ำ
val spots = listOf(
GeoPoint.fromLatLong(35.6586, 139.7454), // Tokyo Tower
GeoPoint.fromLatLong(35.7101, 139.8107), // Skytree
GeoPoint.fromLatLong(35.6852, 139.7528), // Imperial Palace
)
// Start from an empty rectangle and widen it one point at a time
val bounds = GeoRectBounds()
spots.forEach { bounds.extend(it) }
MapLibreMapView(state = mapViewState) {
spots.forEachIndexed { i, point ->
Marker(MarkerState(id = "spot-$i", position = point))
}
}
Button(onClick = {
// padding is screen margin in logical pixels; at 0 the outermost point hugs the edge
if (!bounds.isEmpty) mapViewState.fitBounds(bounds, padding = 64)
}) {
Text("Show all")
}