2012-04-03 103 views
3

聽到的是我的project無法標記

在我的項目我顯示我的當前位置,顯示當前的經緯度long.But我不能 標記我目前在android中的位置。

Thnx提前。

@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 
    Log.d(TAG, "onLocationChanged with location " + location.toString()); 
    // Displays lat, long, altitude and bearing 
    String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getBearing()); 
    this.locationText.setText(text); 

    try { 
     // This gets a list of addresses 
     List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), 
       location.getLongitude(), 10); 
     for (Address address : addresses) { 
      this.locationText.append("\n" + "kaushik"); 
     } 

     // Convert latitude and longitude into int that the GeoPoint constructor can understand 
     int latitude = (int)(location.getLatitude() * 1000000); 
     int longitude = (int)(location.getLongitude() * 1000000); 

     point = new GeoPoint(latitude,longitude); 
     mapController.animateTo(point); 
     mapController.setZoom(16); 

     MapOverlay mapOverLay=new MapOverlay(); 
     mapOverLay.setPointToDraw(point); 
     List<Overlay> listOfOverlays=map.getOverlays(); 
     listOfOverlays.clear(); 
     listOfOverlays.add(mapOverLay); 

     map.invalidate(); 

    } catch (IOException e) { 
     Log.e("LocateMe", "Could not get Geocoder data", e); 
    } 
} 
+0

粘貼代碼,以便我們可以進行更改 – 2012-04-03 09:11:39

+0

@Agarwal我已經啓動了我的項目。 – kaushikSuman 2012-04-03 09:13:46

+0

更好,如果可以將你的sinppet粘貼在這裏將在未來使用完整給其他人 – 2012-04-03 09:19:45

回答

4
mapController.animateTo(point); 
mapController.setZoom(16); 
MapOverlay mapOverLay=new MapOverlay(); 
mapOverLay.setPointToDraw(point); 
List<Overlay> listOfOverlays=map.getOverlays(); 
listOfOverlays.clear(); 
listOfOverlays.add(mapOverLay); 

添加覆蓋圖標替換上述線具有低於線//其中lat和長是從GPS獲取。

Drawable srcdrawable = getApplicationContext().getResources().getDrawable(R.drawable.pin_blue); 
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable, getApplicationContext()); 
GeoPoint srcpoint = new GeoPoint((int)(Double.parseDouble(lat) * 1E6),(int)(Double.parseDouble(lng)* 1E6)); 
OverlayItem srcoverlayitem = new OverlayItem(srcpoint, "Hello!", "This is your Location."); 
srcitemizedOverlay.addOverlay(srcoverlayitem); 
mapView.getOverlays().clear(); 
mapView.getOverlays().add(srcitemizedOverlay); 
mapController.animateTo(srcpoint); 
mapController.setZoom(16); 

//如果你想在地圖上的多個點 /*

Drawable srcdrawable = getApplicationContext().getResources().getDrawable(R.drawable.pin_blue); 
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable, getApplicationContext()); 
forloop(setoflocations){ 
GeoPoint srcpoint = new GeoPoint((int)(Double.parseDouble(lat) * 1E6),(int)(Double.parseDouble(lng)* 1E6)); 
OverlayItem srcoverlayitem = new OverlayItem(srcpoint, "Hello!", "This is your Location."); 
if(srcitemizedOverlay!=null && mapController!=null){ 
srcitemizedOverlay.addOverlay(overlayitem); 
mapController.animateTo(point); 
animatePoint = point; 
} 
} 
mapView.getOverlays().clear(); 
mapView.getOverlays().add(srcitemizedOverlay); 

*/

也使用下面CustomItemizedOverlay.java類

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> { 

    private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>(); 

    private Context context; 

    public CustomItemizedOverlay(Drawable defaultMarker) { 
     super(boundCenterBottom(defaultMarker)); 
    } 

    public CustomItemizedOverlay(Drawable defaultMarker, Context context) { 
     this(defaultMarker); 
     this.context = context; 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     return mapOverlays.get(i); 
    } 

    @Override 
    public int size() { 
     return mapOverlays.size(); 
    } 

    public void addOverlay(OverlayItem overlay) { 
     mapOverlays.add(overlay); 
     this.populate(); 
    } 

} 
+0

另一個問題,我怎麼能標記另一個4-5位置在地圖上使用他們的經緯度。 – kaushikSuman 2012-04-03 10:47:53

+0

@Agarwal爲什麼 animatePoint = point; – kaushikSuman 2012-04-03 11:19:44

+0

它取決於你是否使用它。使用它將導航到最後一個經緯度。這就是所有....也+1爲你查詢哪些將是有用的其他人這就是爲什麼我描述它breifly – 2012-04-03 11:28:08

0

上當前的位置

myItemizedOverlay.addItem(point , "myPoint1", "myPoint1"); 
      m_mapView.getOverlays().add(myItemizedOverlay); 

mapController.animateTo(point);