gpt4 book ai didi

android - 在google map static api中绘制圆形路径

转载 作者:行者123 更新时间:2023-12-02 16:59:42 27 4
gpt4 key购买 nike

我正在开发一个在 android 中使用 map 静态 api 的应用程序

这是业务逻辑,使用这个位置从 google static api 获取用户位置请求并围绕这个位置画一个圆圈

这是我使用的代码

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

现在的问题是如何围绕它画一个圆圈,我搜索了一下,发现它是使用路径完成的,但无法理解如何获得该路径

最佳答案

你只需要像Developers Guide那样绘制路径:

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

您可以用这种方式格式化静态 map 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();
}

圆形路径和填充颜色应设置为 String"0xRRGGBBAA"格式,其中 RR - 红色 channel 的值,GG - 绿色 channel 的值(value),BB - 蓝色 channel 的值和AA - alpha channel 的值(例如 "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);

}
}

你会得到类似的东西:

Static map with circle path

此外,您可以使用 Google Maps Lite Mode而不是静态 map API(精简模式支持绘制圆圈)。或者,如果您需要在 map 的中心精确绘制圆圈 - 直接在位图 Canvas 上绘制。例如你可以修改 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;
}

关于android - 在google map static api中绘制圆形路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54677349/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com