gpt4 book ai didi

java - Toast 名称以及距离 map 中心最近的城市的距离

转载 作者:行者123 更新时间:2023-12-01 12:06:23 25 4
gpt4 key购买 nike

我正在创建瑞典一些城市的 map ,并希望为其添加一些功能。我想显示城市名称以及最接近 map 中心的城市的距离(以公里为单位),我在布局 XML 文件中通过 ImageView 放置了十字准线。有没有正确的方法来实现这一点?

这是我当前用于创建 map 并放置城市标记的代码:

public class MyMap extends Activity implements OnMapReadyCallback
{
public final Context context = this;
private String fileString = "";
private String coordsFileName = "coords";
private GoogleMap myMap = null;
private LatLngBounds bounds = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);

// Gets the map fragment from the xml file
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

// Load strings from file
fileString = ReadFromFile(coordsFileName);
}

@Override
public void onMapReady(GoogleMap map)
{
myMap = map;
List<Marker> markers = new ArrayList<Marker>();

String[] locations = fileString.split(";");
for (String location : locations)
{
try
{
String[] cityLatLng = location.split(":|,");
String cityName = cityLatLng[0];
Double lat = Double.parseDouble(cityLatLng[1]);
Double lng = Double.parseDouble(cityLatLng[2]);
LatLng cityPos = new LatLng(lat, lng);

// Create marker
Marker marker = myMap.addMarker(new MarkerOptions()
.position(cityPos)
.title(cityName));

// Add new marker to array of markers
markers.add(marker);
}
catch(Exception e)
{
System.out.println("Error 3: " + e.getMessage());
}
}

// Move the camera to show all markers
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markers)
{
builder.include(marker.getPosition());
}
bounds = builder.build();

myMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback()
{
@Override
public void onMapLoaded()
{
// Pixel offset from edge of map
int padding = 30;

// Move the camera
myMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
}
});
}

}

这是我的布局 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_locate"
android:layout_gravity="center_vertical"
android:layout_centerInParent="true"
android:contentDescription="@string/crosshairs"/>
</RelativeLayout>

最佳答案

可能的解决方案之一是首先使用以下方法找到 map 的中心坐标

map.getCenter();

这将返回 latlang 对象。然后,您可以使用 Google Distance Matrix API 比较中心与每个位置标记(代表城市)的距离。 ,使用 minDist() 方法找出最小值,并返回最小距离处标记的坐标(即城市名称)

希望这会有所帮助!

关于java - Toast 名称以及距离 map 中心最近的城市的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27563893/

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