我正在创建瑞典一些城市的地图,并希望添加一些功能。我想显示城市名称和距离地图中心最近的城市距离,我在布局XML文件中通过ImageView放置了一个十字准线。有没有正确的方法来实现这个目标?
这是我目前用于创建地图并放置城市标记的代码:
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>
答案 0 :(得分:1)
可能的解决方案之一是首先使用
找到地图的中心坐标map.getCenter();
这将返回latlang对象。然后你可以使用Google Distance Matrix API比较中心与每个位置标记(代表城市)的距离,使用minDist()方法找出min并返回最小距离的标记坐标(是城市的名称)
希望这会有所帮助!!!
答案 1 :(得分:0)
我实际上设法使用Location对象和GoogleMap.setOnCameraChangeListener()来解决它,以便每次用户导航地图时重新计算。下面是我为实现这一目标而设法编写的代码。
myMap.setOnCameraChangeListener(new OnCameraChangeListener()
{
@SuppressLint("DefaultLocale") @Override
public void onCameraChange(CameraPosition position)
{
// Get the latlng of the map center
LatLng mapCenter = myMap.getCameraPosition().target;
// Create a centerlocation based on the map's latlng
Location centerLocation = new Location("CenterLocation");
centerLocation.setLatitude(mapCenter.latitude);
centerLocation.setLongitude(mapCenter.longitude);
// Location for storage of the city closest to the map
Location closestCity = new Location("ClosestCity");
float distance = 0;
for (City city : cities)
{
Location cityLocation = new Location(city.getCityName());
cityLocation.setLatitude(city.getLatitude());
cityLocation.setLongitude(city.getLongitude());
float currentCityDistance = cityLocation.distanceTo(centerLocation);
if(distance == 0)
{
distance = currentCityDistance;
closestCity = cityLocation;
}
if(currentCityDistance < distance)
{
distance = currentCityDistance;
closestCity = cityLocation;
}
}
// Convert from meters to kilometers
float distanceKm = distance / 1000;
String kilometersString = String.format("%.2f", distanceKm);
// Present a toast with information
Toast.makeText(getBaseContext(), "Distance to " + closestCity.getProvider() + ": " + kilometersString + " km", Toast.LENGTH_SHORT).show();
}
});