-2

我正在嘗試製作一個應用程序,顯示當前位置具有多個標記的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; 
} 

}

+1

究竟是什麼問題的代碼? – nasch

+0

該代碼是正確的,但我不知道如何設置,顯示我最近的地方我的當前位置 –

+0

我沒有看到您的requestLocationUpdates(...),你手動調用onLocationChanged(...)與LM .getLastKnownLocation(提供商); ...它工作嗎?如果你沒有請求更新,我認爲getLastKnownLocation不應該返回一個位置。如果使用requestLocationUpdates設置LocationListener,則會在locatonUpdates上自動調用LocationListener的onLocationChanged方法。 – Damien

回答

0

我很抱歉,但我不這裏真的很瞭解你的問題。看來你知道如何用你的MyMarker對象使用HashMap恢復地圖標記。你也可以創建你需要的所有標記。由於你的問題是關於使用HashMap和多個標記創建一個應用程序,看起來你已經做到了。除非你的代碼不起作用,但在這種情況下,如果你告訴我們它究竟做了什麼,這將是有益的。

我不確定你在問什麼,但我相信你有5個標記,你想得到最接近的標記。如果你的問題是如何做到這一點,那麼我認爲你只需循環每一個標記,並計算他們與用戶的位置的距離,以找到最小的一個。

如果你不知道如何計算距離,我personnaly使用位置的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; 
    } 
} 

迅速寫這並沒有測試它,但如果你我想這是可行的:

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

我希望該應用程序顯示5個最接近的標記,因爲我將擴展HashMap上的標記數。我怎麼能應用calucalte的方法你顯示我的例子的距離? –

+0

我如何在我的應用程序上應用distanceTo?除了我必須循環每個標記之外,還有什麼方法嗎? –

+0

「如果你的問題是如何做到這一點,那麼我認爲你只需循環每個標記,並計算它們與用戶位置的距離,以找到最小的那個。」我同意,沒有其他方法可以找到最接近特定位置的標記。 – nasch

相關問題