แนวคิดขั้นสูง

กำลังรับข้อมูล

การรับข้อมูลตำแหน่งที่รวบรวมไว้ทำได้หลายวิธี เราได้อธิบายเทคนิค 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 ที่โหลดลงบนแผนที่

ข้อมูลดิบ 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 แสดงรหัสสถานที่เมื่อสแนปไปยังถนนคือสามารถใช้รหัสสถานที่ใน 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