我正在嘗試製作一個應用程序,顯示當前位置具有多個標記的5個最近位置。在其他主題上,我看到了很多示例,但是,沒有人解釋如何使用多個標記或HashMap創建應用程序,以顯示最近的地方。請幫我解決它。如何使android應用程序顯示具有多個標記的最近位置
這是我的主要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;
}
}
究竟是什麼問題的代碼? – nasch
該代碼是正確的,但我不知道如何設置,顯示我最近的地方我的當前位置 –
我沒有看到您的requestLocationUpdates(...),你手動調用onLocationChanged(...)與LM .getLastKnownLocation(提供商); ...它工作嗎?如果你沒有請求更新,我認爲getLastKnownLocation不應該返回一個位置。如果使用requestLocationUpdates設置LocationListener,則會在locatonUpdates上自動調用LocationListener的onLocationChanged方法。 – Damien