กําลังรับข้อมูล
การรับข้อมูลตําแหน่งที่รวบรวมไว้ทําได้หลายวิธี เราจะอธิบายเทคนิค 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 จุดต่อคําขอ) แทนที่จะต้องคํานึงถึงจุดใดจุดหนึ่ง
คุณควรรวมบางคําขอซ้อนทับกันเพื่อให้คําขอแต่ละรายการเป็นเส้นทางยาว 1 คะแนน เพื่อให้ระบบรวมคะแนนสุดท้ายจากคําขอก่อนหน้าเป็นจุดแรกของคําขอที่ตามมา จํานวนคะแนนจะขึ้นอยู่กับ ความถูกต้องของข้อมูลของคุณ คุณควรเพิ่มคะแนน สําหรับคําขอที่มีความแม่นยําต่ํา
ตัวอย่างนี้ใช้ไคลเอ็นต์ 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; }
ในการทําเครื่องหมายเครื่องหมายขีดจํากัดความเร็วด้วยที่อยู่จาก Geocoding API
รหัสตัวอย่าง
ข้อควรพิจารณา
โค้ดที่สนับสนุนบทความนี้เป็นแอป Android เพียงแอปเดียวเพื่อการอธิบาย ในทางปฏิบัติ คุณไม่ควรแจกจ่ายคีย์ API ฝั่งเซิร์ฟเวอร์ในแอป Android เนื่องจากคีย์ของคุณไม่สามารถป้องกันการเข้าถึงที่ไม่ได้รับอนุญาตจากบุคคลที่สามได้ คุณควรใช้โค้ดฝั่ง API เป็นพร็อกซีฝั่งเซิร์ฟเวอร์แทนและให้แอป Android ส่งคําขอผ่านพร็อกซีเพื่อให้มั่นใจว่าคีย์ได้รับอนุญาต
ดาวน์โหลด
ดาวน์โหลดโค้ดจาก GitHub