กำลังรับข้อมูล
วิธีการรับข้อมูลตำแหน่งที่เก็บรวบรวมไว้มีหลายวิธี เราจะอธิบายเทคนิค 2 อย่างในการดึงข้อมูลเพื่อใช้กับฟีเจอร์สแนปไปยังถนนของ Roads API
GPX
GPX เป็นรูปแบบ XML แบบเปิดสำหรับการแชร์เส้นทาง เส้นทาง และจุดอ้างอิงที่บันทึกโดยอุปกรณ์ GPS ตัวอย่างนี้ใช้โปรแกรมแยกวิเคราะห์ XmlPull ซึ่งเป็นโปรแกรมแยกวิเคราะห์ XML ขนาดเล็กที่ใช้ได้กับทั้งสภาพแวดล้อมเซิร์ฟเวอร์ Java และอุปกรณ์เคลื่อนที่
/** * Parses the waypoint (wpt tags) data into native objects from a GPX stream. */ private List<LatLng> loadGpxData(XmlPullParser parser, InputStream gpxIn) throws XmlPullParserException, IOException { // We use a List<> as we need subList for paging later List<LatLng> latLngs = new ArrayList<>(); parser.setInput(gpxIn, null); parser.nextTag(); while (parser.next() != XmlPullParser.END_DOCUMENT) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } if (parser.getName().equals("wpt")) { // Save the discovered latitude/longitude attributes in each <wpt>. latLngs.add(new LatLng( Double.valueOf(parser.getAttributeValue(null, "lat")), Double.valueOf(parser.getAttributeValue(null, "lon")))); } // Otherwise, skip irrelevant data } return latLngs; }
นี่คือข้อมูลดิบ GPX ที่โหลดบนแผนที่
บริการตำแหน่งของ Android
วิธีที่ดีที่สุดในการบันทึกข้อมูล GPS จากอุปกรณ์ Android จะแตกต่างกันไปตามกรณีการใช้งาน ดูชั้นเรียนการฝึกอบรมของ Android เกี่ยวกับการรับการอัปเดตตำแหน่ง รวมถึงตัวอย่างตำแหน่งของ Google Play ใน GitHub
การประมวลผลเส้นทางยาว
เนื่องจากฟีเจอร์สแนปไปยังถนนอนุมานตำแหน่งโดยอิงตามเส้นทางทั้งหมด ไม่ใช่จุดใดจุดหนึ่ง คุณจึงต้องระมัดระวังเมื่อประมวลผลเส้นทางยาว (กล่าวคือ เส้นทางที่เกินขีดจำกัด 100 จุดต่อคำขอ)
วิธีพิจารณาว่าคำขอแต่ละรายการเป็นเส้นทางยาวเส้นเดียว คุณควรรวมส่วนที่ทับซ้อนกันไว้เพื่อให้รวมประเด็นสุดท้ายจากคำขอก่อนหน้าไว้เป็นจุดแรกของคำขอที่ตามมา จำนวนคะแนนที่จะรวม จะขึ้นอยู่กับความถูกต้องของข้อมูล คุณควรใส่คะแนนเพิ่ม สำหรับคำขอที่มีความแม่นยำต่ำ
ตัวอย่างนี้ใช้ไคลเอ็นต์ Java สำหรับบริการ Google Maps เพื่อส่งคำขอแบบแบ่งหน้า และจากนั้นกลับเข้าข้อมูลอีกครั้ง รวมถึงค่าที่ประมาณไว้ลงในรายการที่แสดงผล
/** * Snaps the points to their most likely position on roads using the Roads API. */ private List<SnappedPoint> snapToRoads(GeoApiContext context) throws Exception { List<SnappedPoint> snappedPoints = new ArrayList<>(); int offset = 0; while (offset < mCapturedLocations.size()) { // Calculate which points to include in this request. We can't exceed the API's // maximum and we want to ensure some overlap so the API can infer a good location for // the first few points in each request. if (offset > 0) { offset -= PAGINATION_OVERLAP; // Rewind to include some previous points. } int lowerBound = offset; int upperBound = Math.min(offset + PAGE_SIZE_LIMIT, mCapturedLocations.size()); // Get the data we need for this page. LatLng[] page = mCapturedLocations .subList(lowerBound, upperBound) .toArray(new LatLng[upperBound - lowerBound]); // Perform the request. Because we have interpolate=true, we will get extra data points // between our originally requested path. To ensure we can concatenate these points, we // only start adding once we've hit the first new point (that is, skip the overlap). SnappedPoint[] points = RoadsApi.snapToRoads(context, true, page).await(); boolean passedOverlap = false; for (SnappedPoint point : points) { if (offset == 0 || point.originalIndex >= PAGINATION_OVERLAP - 1) { passedOverlap = true; } if (passedOverlap) { snappedPoints.add(point); } } offset = upperBound; } return snappedPoints; }
นี่คือข้อมูลจากด้านบนหลังจากเรียกใช้การสแนปไปยังคําขอบนถนน เส้นสีแดงคือข้อมูลดิบ และเส้นสีน้ำเงินคือข้อมูลที่สแนป
การใช้โควต้าอย่างมีประสิทธิภาพ
การตอบกลับคำขอสแนปไปยังถนนจะรวมรายการรหัสสถานที่ที่แมปกับจุดที่คุณระบุไว้ ซึ่งอาจมีจุดเพิ่มเติมหากคุณตั้งค่า interpolate=true
คุณควรค้นหาเฉพาะรหัสสถานที่ที่ไม่ซ้ำในคำขอเท่านั้น เพื่อให้ใช้โควต้าที่ได้รับอนุญาตสำหรับคำขอจำกัดความเร็วได้อย่างมีประสิทธิภาพ ตัวอย่างนี้ใช้ไคลเอ็นต์ Java สำหรับบริการ Google Maps เพื่อค้นหาขีดจำกัดความเร็วจากรายการรหัสสถานที่
/** * Retrieves speed limits for the previously-snapped points. This method is efficient in terms * of quota usage as it will only query for unique places. * * Note: Speed limit data is only available for requests using an API key enabled for a * Google Maps APIs Premium Plan license. */ private Map<String, SpeedLimit> getSpeedLimits(GeoApiContext context, List<SnappedPoint> points) throws Exception { Map<String, SpeedLimit> placeSpeeds = new HashMap<>(); // Pro tip: Save on quota by filtering to unique place IDs. for (SnappedPoint point : points) { placeSpeeds.put(point.placeId, null); } String[] uniquePlaceIds = placeSpeeds.keySet().toArray(new String[placeSpeeds.keySet().size()]); // Loop through the places, one page (API request) at a time. for (int i = 0; i < uniquePlaceIds.length; i += PAGE_SIZE_LIMIT) { String[] page = Arrays.copyOfRange(uniquePlaceIds, i, Math.min(i + PAGE_SIZE_LIMIT, uniquePlaceIds.length)); // Execute! SpeedLimit[] placeLimits = RoadsApi.speedLimits(context, page).await(); for (SpeedLimit sl : placeLimits) { placeSpeeds.put(sl.placeId, sl); } } return placeSpeeds; }
นี่เป็นข้อมูลจากด้านบนซึ่งมีการระบุขีดจำกัดความเร็วไว้ที่รหัสสถานที่ที่ไม่ซ้ำกันแต่ละรหัส
แทรกแซง API อื่นๆ
ข้อดีอย่างหนึ่งของการมีการแสดงรหัสสถานที่ในการตอบสนองสแนปไปยังถนนคือคุณสามารถใช้รหัสสถานที่กับ Google Maps Platform API หลายๆ ตัวได้ ตัวอย่างนี้ใช้ไคลเอ็นต์ Java สำหรับบริการ Google Maps เพื่อเข้ารหัสพิกัดภูมิศาสตร์ของสถานที่ที่ถูกส่งคืนจากคำขอสแนปไปยังถนนด้านบน
/** * Geocodes a snapped point using the place ID. */ private GeocodingResult geocodeSnappedPoint(GeoApiContext context, SnappedPoint point) throws Exception { GeocodingResult[] results = GeocodingApi.newRequest(context) .place(point.placeId) .await(); if (results.length > 0) { return results[0]; } return null; }
ซึ่งเครื่องหมายขีดจำกัดความเร็วได้ใส่คำอธิบายประกอบจากที่อยู่จาก Geoที่อยู่ในรายการ API แล้ว
รหัสตัวอย่าง
ข้อควรพิจารณา
โค้ดสนับสนุนบทความนี้มีให้บริการเป็นแอป Android แอปเดียวที่มีไว้เพื่อการอธิบาย ในทางปฏิบัติ คุณไม่ควรแจกจ่ายคีย์ API ฝั่งเซิร์ฟเวอร์ในแอป Android เนื่องจากคีย์ของคุณไม่สามารถรักษาความปลอดภัยจากการเข้าถึงโดยไม่ได้รับอนุญาตจากบุคคลที่สามได้ เพื่อรักษาความปลอดภัยให้คีย์ของคุณ คุณควรติดตั้งใช้งานโค้ดที่ใช้ API เป็นพร็อกซีฝั่งเซิร์ฟเวอร์ และให้แอป Android ส่งคำขอผ่านพร็อกซีเพื่อตรวจสอบว่าคำขอนั้นได้รับอนุญาต
ดาวน์โหลด
ดาวน์โหลดโค้ดจาก GitHub