簡體   English   中英

在谷歌地圖靜態api中繪制圓形路徑

[英]Draw a circle path in google map static api

我正在使用 android 中的地圖靜態 api 開發一個應用程序

這是業務邏輯,使用此位置從谷歌靜態 api 獲取用戶位置請求並在此位置周圍畫一個圓圈

這是我正在使用的代碼

https://maps.googleapis.com/maps/api/staticmap?center=29.31166,47.481766&zoom=7&size=600x300&maptype=roadmap&key=My Key

現在的問題是如何在它周圍畫一個圓圈,我搜索並發現它是使用路徑完成的,但無法理解如何獲得該路徑

您只需要在開發人員指南中繪制路徑:

http://maps.googleapis.com/maps/api/staticmap?center=29.31166,47.48177&zoom=7&size=600x300&path=color:0x0000FFFF|weight:3|fillcolor:0x0000FF77|<FIRST_POINT_LAT>,<FIRST_POINT_LNG>|<SECOND_POINT_LAT>,<SECOND_POINT_LNG>|...|<LAST_POINT_LAT>,<LAST_POINT_LNG>&key=<YOUR_API_KEY>

其中<FIRST_POINT_LAT>,<FIRST_POINT_LNG>|<SECOND_POINT_LAT>,<SECOND_POINT_LNG>|...|<LAST_POINT_LAT>,<LAST_POINT_LNG>是圓路徑的坐標。 對於它的計算,您可以使用這樣的方法:

private List<LatLng> getCirclePoints(LatLng center, double radius) {
    List<LatLng> circlePoints = new ArrayList<>();

    // convert center coordinates to radians
    double lat_rad = Math.toRadians(center.latitude);
    double lon_rad = Math.toRadians(center.longitude);
    double dist = radius / 6378137;

    // calculate circle path point for each 5 degrees
    for (int deg = 0; deg < 360; deg += 5) {
        double rad = Math.toRadians(deg);

        // calculate coordinates of next circle path point
        double new_lat = Math.asin(Math.sin(lat_rad) * Math.cos(dist) + Math.cos(lat_rad) * Math.sin(dist) * Math.cos(rad));
        double new_lon = lon_rad + Math.atan2(Math.sin(rad) * Math.sin(dist) * Math.cos(lat_rad), Math.cos(dist)
                - Math.sin(lat_rad) * Math.sin(new_lat));

        // convert new lat and lon to degrees
        double new_lat_deg = Math.toDegrees(new_lat);
        double new_lon_deg = Math.toDegrees(new_lon);

        circlePoints.add(new LatLng(new_lat_deg, new_lon_deg));
    }

    return circlePoints;
}

您可以通過以下方式使用該點格式化靜態地圖 API URL:

private String buildStaticApiUrlWithCircle(LatLng mapCenter, int zoom, int width, int height,
                                           LatLng circleCenter, double circleRadius, int pathWeight, String pathColor, String fillColor) {

    List<LatLng> circlePoints =getCirclePoints(circleCenter, circleRadius);

    StringBuilder url = new StringBuilder();
    url.append("http://maps.googleapis.com/maps/api/staticmap?");
    url.append(String.format("center=%8.5f,%8.5f", mapCenter.latitude, mapCenter.longitude));
    url.append(String.format("&zoom=%d", zoom));
    url.append(String.format("&size=%dx%d", width, height));

    // set circle path properties
    url.append(String.format("&path="));
    url.append(String.format("color:%s", pathColor));
    url.append(String.format("|weight:%d", pathWeight));
    url.append(String.format("|fillcolor:%s", fillColor));

    // add circle path points
    for (LatLng point : circlePoints) {
        url.append(String.format("|%8.5f,%8.5f", point.latitude, point.longitude));
    }

    // add API key to URL
    url.append(String.format("&key=%s", <YOUR_API_KEY>)));
    return url.toString();
}

圓形路徑和填充顏色應設置為"0xRRGGBBAA"格式的String ,其中RR - 紅色通道的值, GG - 綠色通道的值, BB - 藍色通道的值和AA - Alpha 通道的值(例如"0x0000FFFF" -純藍色不透明, "0xFF000077" - 純紅色 50% 透明等)。

當您以這種方式使用buildStaticApiUrlWithCircle()

...
int mapZoom = 7;
int mapWidth = 600;
int mapHeight = 300;
LatLng mapCenter = new LatLng(29.31166, 47.481766);

LatLng circleCenter = new LatLng(29.376297, 47.976379);
double circleRadiusMerers = 35000;
String circlePathColor = "0x0000FFFF";
String circleFillColor = "0x0000FF99";

String mapUrl = buildStaticApiUrlWithCircle(mapCenter, mapZoom, mapWidth, mapHeight,
                                            circleCenter, circleRadiusMerers, 3, circlePathColor, circleFillColor);

try {
    Bitmap mapBitmap = new GetStaticMapAsyncTask().execute(mapUrl).get();
    mMapImageView.setImageBitmap(mapBitmap);
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (ExecutionException e) {
    e.printStackTrace();
}
...

其中GetStaticMapAsyncTask是:

private class GetStaticMapAsyncTask extends AsyncTask<String, Void, Bitmap> {

    protected void onPreExecute() {
        super.onPreExecute();
    }

    protected Bitmap doInBackground(String... params) {

        Bitmap bitmap = null;
        HttpURLConnection connection = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            int responseCode = connection.getResponseCode();

            InputStream stream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(stream);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }

        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);

    }
}

你會得到這樣的東西:

帶有圓形路徑的靜態地圖

此外,您可以使用Google Maps Lite 模式代替靜態地圖 API(Lite 模式支持繪制圓圈)。 甚至,如果您需要精確地在地圖的中心繪制圓圈 - 直接在位圖畫布上繪制。 例如,你可以修改doInBackground()GetStaticMapAsyncTask這種方式:

protected Bitmap doInBackground(String... params) {

    Bitmap bitmap = null;
    HttpURLConnection connection = null;

    try {
        URL url = new URL(params[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        int responseCode = connection.getResponseCode();
        InputStream stream = connection.getInputStream();
        Bitmap mapBitmap = BitmapFactory.decodeStream(stream);

        Paint locaionMarkerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        locaionMarkerPaint.setColor(Color.BLUE);

        bitmap = Bitmap.createBitmap(mapBitmap.getWidth(), mapBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(mapBitmap,0,0, null);
        canvas.drawCircle(mapBitmap.getWidth()/ 2, mapBitmap.getHeight() / 2, 20, locaionMarkerPaint);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }

    return bitmap;
}

如果我正確理解你的話,只需要在onMapReady()中添加此on方法

Circle circle = map.addCircle(new CircleOptions()
     .center(new LatLng(latitude, longitude))
     .radius(10000)
     .strokeColor(Color.RED)
     .fillColor(Color.BLUE));

Googlemap類中的地圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM