উন্নত ধারণা

ডেটা অর্জন

There are many ways to obtain collected location data. এখানে আমরা Roads API স্ন্যাপ টু রোডস বৈশিষ্ট্যের সাথে ডেটা ব্যবহার করার জন্য ডেটা অর্জনের জন্য দুটি কৌশল বর্ণনা করি।

জিপিএক্স

GPX is an open XML-based format for sharing routes, tracks and waypoints captured by GPS devices. This example uses the XmlPull parser, a lightweight XML parser available for both Java server and mobile environments.

/**
 * 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;
}

এখানে একটি মানচিত্রে লোড করা কিছু কাঁচা জিপিএক্স ডেটা রয়েছে।

Raw GPX data on a map

Android location services

অ্যান্ড্রয়েড ডিভাইস থেকে জিপিএস ডেটা ক্যাপচারের সর্বোত্তম উপায় আপনার ব্যবহারের ক্ষেত্রে নির্ভর করে পরিবর্তিত হয়। Take a look at the Android training class on Receiving Location Updates , as well as the Google Play Location samples on GitHub .

দীর্ঘ পথ প্রক্রিয়া করুন

As the snap to roads feature infers the location based on the full path, rather than individual points, you need to take care when processing long paths (that is, paths over the 100-point-per-request limit).

স্বতন্ত্র অনুরোধগুলিকে একটি দীর্ঘ পথ হিসাবে বিবেচনা করার জন্য, আপনার কিছু ওভারল্যাপ অন্তর্ভুক্ত করা উচিত, যেমন পূর্ববর্তী অনুরোধের চূড়ান্ত পয়েন্টগুলি পরবর্তী অনুরোধের প্রথম পয়েন্ট হিসাবে অন্তর্ভুক্ত করা হয়েছে। পয়েন্টের সংখ্যা আপনার ডেটার নির্ভুলতার উপর নির্ভর করে। কম-নির্ভুলতার অনুরোধের জন্য আপনার আরও পয়েন্ট অন্তর্ভুক্ত করা উচিত।

This example uses the Java Client for Google Maps Services to send paged requests and then rejoins the data, including interpolated points, into the returned list.

/**
 * 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;
}

স্ন্যাপ চালানোর পরে রাস্তার অনুরোধগুলি চালানোর পরে উপরে থেকে ডেটা এখানে। লাল রেখাটি কাঁচা ডেটা এবং নীল রেখাটি স্ন্যাপড ডেটা।

ডেটার উদাহরণ যা রাস্তাগুলিতে স্ন্যাপ করা হয়েছে৷

Efficient use of quota

রাস্তার অনুরোধের একটি স্ন্যাপের প্রতিক্রিয়াতে স্থানের আইডিগুলির একটি তালিকা অন্তর্ভুক্ত রয়েছে যা আপনার দেওয়া পয়েন্টগুলিতে ম্যাপ করে, সম্ভাব্যভাবে অতিরিক্ত পয়েন্ট সহ যদি আপনি interpolate=true সেট করেন।

গতি সীমাবদ্ধতার অনুরোধের জন্য আপনার অনুমোদিত কোটার দক্ষ ব্যবহার করার জন্য, আপনার অনুরোধে আপনার কেবল অনন্য স্থান আইডিগুলির জন্য জিজ্ঞাসা করা উচিত। This example uses the Java Client for Google Maps Services to query speed limits from a list of place IDs.

/**
 * 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;
}

Here's the data from above with speed limits marked at each unique place ID.

একটি মানচিত্রে গতি সীমা লক্ষণ

অন্যান্য এপিআইয়ের সাথে ইন্টারপ্লে

One of the benefits of having place IDs returned in the snap to roads responses is that you can use the place ID across many of the Google Maps Platform APIs. This example uses the Java Client for Google Maps Services to geocode a place returned from the above snap to road request.

/**
 * 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;
}

এখানে গতি সীমা চিহ্নিতকারী জিওকোডিং এপিআই থেকে ঠিকানা সহ টীকা করা হয়েছে।

একটি চিহ্নিতকারীতে জিওকোডযুক্ত ঠিকানা দেখানো হয়েছে

নমুনা কোড

বিবেচনা

এই দস্তাবেজ সমর্থনকারী কোডটি চিত্রণমূলক উদ্দেশ্যে একক অ্যান্ড্রয়েড অ্যাপ্লিকেশন হিসাবে উপলব্ধ। In practice you shouldn't distribute your server-side API keys in an Android app as your key cannot be secured against unauthorized access from a third party. পরিবর্তে, আপনার কীগুলি সুরক্ষিত করার জন্য আপনাকে সার্ভার-সাইড প্রক্সি হিসাবে API-মুখী কোড স্থাপন করা উচিত এবং অনুরোধগুলি অনুমোদিত হয়েছে তা নিশ্চিত করতে আপনার Android অ্যাপকে প্রক্সি ব্যবহার করে অনুরোধ পাঠাতে হবে।

ডাউনলোড করুন

গিথুব থেকে কোডটি ডাউনলোড করুন।