我正在尝试创建一个应用程序,它显示距离我当前位置有多个标记的5个最近的位置。在其他主题上我看到了很多例子,但是,没有人解释如何创建具有多个标记的应用程序或显示最近位置的HashMap。请帮我解决一下。
这是我的主要activity.java
public class MainActivity extends Activity
{
private GoogleMap mMap;
private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
private HashMap<Marker, MyMarker> mMarkersHashMap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMarkersHashMap = new HashMap<Marker, MyMarker>();
mMyMarkersArray.add(new MyMarker("Restaurant Zaplet", "Telephone: \n Address: ", "iconZaplet", Double.parseDouble("43.381499"), Double.parseDouble("19.808931")));
mMyMarkersArray.add(new MyMarker("Restaurant Oresac", "Telephone: \n Address: ","iconOresac", Double.parseDouble("43.796009"), Double.parseDouble("21.745934")));
mMyMarkersArray.add(new MyMarker("Restaurant Obilic","Telephone: \n Address: ", "iconObilic", Double.parseDouble("42.547670"), Double.parseDouble("19.660000")));
mMyMarkersArray.add(new MyMarker("Restaurant Dva sesira","Telephone: \n Address: XII vek", "iconDvaSesira", Double.parseDouble("41.486736"), Double.parseDouble("20.731670")));
mMyMarkersArray.add(new MyMarker("Manastir Lipov Lad", "Telephone: \n Address: ","iconLipovLad", Double.parseDouble("44.850546"), Double.parseDouble("20.479869")));
mMyMarkersArray.add(new MyMarker("Restaurant Slodes", "Telephone: nepoznato \n Address: ", "iconSlodes", Double.parseDouble("41.503238"), Double.parseDouble("19.791890")));
setUpMap();
plotMarkers(mMyMarkersArray);
}
private void plotMarkers(ArrayList<MyMarker> markers)
{
if(markers.size() > 0)
{
for (MyMarker myMarker : markers)
{
MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
markerOption.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_restaurant));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
private int manageMarkerIcon(String markerIcon)
{
if (markerIcon.equals("iconZaplet"))
return R.drawable.zaplet;
else if(markerIcon.equals("iconOresac"))
return R.drawable.oresac;
else if(markerIcon.equals("iconObilic"))
return R.drawable.obilic;
else if(markerIcon.equals("iconLipovLad"))
return R.drawable.lipovlad;
else if(markerIcon.equals("iconSlodes"))
return R.drawable.slodes;
else
return R.drawable.icondefault;
}
private void setUpMap()
{
if (mMap == null)
{
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null)
{
mMap.setMyLocationEnabled(true);
LocationManager lm = (LocationManager) getSystemService (LOCATION_SERVICE);
String provider = lm.getBestProvider(new Criteria (), true);
if (provider == null) {
onProviderDisabled (provider);
}
Location loc = lm.getLastKnownLocation(provider);
if (loc != null) {
onLocationChanged (loc);
}
mMap.setOnMapLongClickListener(onLongClickMapSettings ());
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
{
@Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker)
{
marker.showInfoWindow();
return true;
}
});
}
else
Toast.makeText(getApplicationContext(), "Unable to create Maps", Toast.LENGTH_SHORT).show();
}
}
private OnMapLongClickListener onLongClickMapSettings() {
// TODO Auto-generated method stub
return new OnMapLongClickListener () {
@Override
public void onMapLongClick(LatLng arg0) {
// TODO Auto-generated method stub
Log.i(arg0.toString(), "User long clicked");
}
};
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
LatLng latlng = new LatLng (location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter
{
public MarkerInfoWindowAdapter()
{
}
@Override
public View getInfoWindow(Marker marker)
{
return null;
}
@SuppressLint("InflateParams") @Override
public View getInfoContents(Marker marker)
{
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
MyMarker myMarker = mMarkersHashMap.get(marker);
ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
TextView markerLabel = (TextView)v.findViewById(R.id.marker_label);
TextView anotherLabel = (TextView)v.findViewById(R.id.another_label);
markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));
markerLabel.setText(myMarker.getmLabel());
anotherLabel.setText(myMarker.getmIstorijat());
return v;
}
}
}
以下是MyMarker.java的代码
> public class MyMarker
{
private String mLabel;
private String mIstorijat;
private String mIcon;
private Double mLatitude;
private Double mLongitude;
public MyMarker(String label, String istorijat, String icon, Double latitude, Double longitude)
{
this.mLabel = label;
this.mIstorijat = istorijat;
this.mLatitude = latitude;
this.mLongitude = longitude;
this.mIcon = icon;
}
public String getmLabel()
{
return mLabel;
}
public void setmLabel(String mLabel)
{
this.mLabel = mLabel;
}
public String getmIstorijat()
{
return mIstorijat;
}
public void setmIstorijat(String mIstorijat)
{
this.mIstorijat = mIstorijat;
}
public String getmIcon()
{
return mIcon;
}
public void setmIcon(String icon)
{
this.mIcon = icon;
}
public Double getmLatitude()
{
return mLatitude;
}
public void setmLatitude(Double mLatitude)
{
this.mLatitude = mLatitude;
}
public Double getmLongitude()
{
return mLongitude;
}
public void setmLongitude(Double mLongitude)
{
this.mLongitude = mLongitude;
}
}
答案 0 :(得分:0)
我很抱歉,但我真的不明白你的问题。您似乎知道如何使用HashMap使用MyMarker对象恢复地图标记。而且您还可以创建所需的所有标记。由于您的问题是关于使用HashMap和多个标记创建应用程序,您似乎已经这样做了。除非您的代码不起作用,但在这种情况下,如果您告诉我们它的确切功能,那将会很有帮助。
我不确定你在问什么,但我相信你有5个标记,你想得到最接近的标记。如果您的问题是如何做到这一点,那么我认为您只需循环每个标记并计算它们与用户位置的距离,以找到最小的标记。
如果您不知道如何计算距离,我个人使用位置的distanceTo(Location)方法。也许这不是最好的代码,但这就是它的样子:
public static double distanceTo(LatLng from, LatLng to) {
Location locationA = new Location("");
locationA.setLatitude(from.latitude);
locationA.setLongitude(from.longitude);
Location locationB = new Location("");
locationB.setLatitude(to.latitude);
locationB.setLongitude(to.longitude);
return locationA.distanceTo(locationB) / 1000;
}
你可以做一些类似的事情:
Marker closest = null;
double minDistance;
for(Marker m : mMarkersHashMap.keySet()){
double distance = distanceTo(currentPos, m.getPosition())
if(closest == null || minDistance > distance){
closest = m;
minDistance = distance;
}
}
快速写完并且没有测试过,但是如果你想对5个最接近的标记进行测试,我认为这样可行:
Marker[] closest = new Marker[5];
double[] minDistance = new double[5];
for(Marker m : mMarkersHashMap.keySet()){
double distance = distanceTo(currentPos, m.getPosition())
for(int i = 4; i >=0; i--){
if(closest[i] == null || minDistance[i] > distance){
if(i < 4){
closest[i+1] = closest[i];
minDistance[i+1] = minDistance[i];
}
closest[i] = m;
minDistance[i] = distance;
} else {
break;
}
}
}