2012-08-12 24 views
0

我需要證明我的current locationMarker總是在Google Map使用ItemizedOverlay但每當我運行下面的代碼,我的標記不露面如果我改變從DDMS位置。以下是我的代碼。我正在做什麼錯?標記沒有顯示在當前位置

public class MainActivity extends MapActivity { 
    private MyItemizedOverlay myItemizedOverlay = null; 
    private LocationManager locationManager = null; 
    private MapView mapView = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mapView = (MapView) findViewById(R.id.mapView); 
     mapView.setBuiltInZoomControls(true); 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
       0, new GeoUpdateHandler()); 

    } 


    public class GeoUpdateHandler implements LocationListener { 

    @Override 
    public void onLocationChanged(Location location) { 
     int lat = (int) (location.getLatitude() * 1E6); 
     int lng = (int) (location.getLongitude() * 1E6); 

     Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on); 
     int markerWidth = marker.getIntrinsicWidth(); 
     int markerHeight = marker.getIntrinsicHeight(); 
     marker.setBounds(0, markerHeight, markerWidth, 0); 


     myItemizedOverlay = new MyItemizedOverlay(marker); 
     mapView.getOverlays().add(myItemizedOverlay); 


     GeoPoint point = new GeoPoint(lat, lng); 

     //GeoPoint myPoint1 = new GeoPoint((int) (37.347184*1000000), (int) (-121.966551*1000000)); 
     myItemizedOverlay.addItem(point, "myPoint1", "myPoint1"); 
     mapController.animateTo(point); 
     String address = ConvertPointToLocation(point); 
     Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show(); 

     mapView.invalidate(); 

    } 


} 

下面是MyItemizedOverlay class

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{ 

    private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>(); 

    public MyItemizedOverlay(Drawable marker) { 
     super(boundCenterBottom(marker)); 
     // TODO Auto-generated constructor stub 

     populate(); 
    } 

    public void addItem(GeoPoint p, String title, String snippet){ 
     OverlayItem newItem = new OverlayItem(p, title, snippet); 
     overlayItemList.add(newItem); 
     populate(); 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     // TODO Auto-generated method stub 
     return overlayItemList.get(i); 
    } 

    @Override 
    public int size() { 
     // TODO Auto-generated method stub 
     return overlayItemList.size(); 
    } 

    @Override 
    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
     // TODO Auto-generated method stub 
     super.draw(canvas, mapView, shadow); 
     //boundCenterBottom(marker); 
    } 

} 

,因爲我需要顯示其他標記的也對谷歌地圖,這樣才符合ItemizedOverlay概念我去的原因。首先,我需要專注於當前位置,每當我的應用程序啓動時總是使用ItemizedOverlay。我的例子的任何建議將不勝感激。

回答

1

如果您只想顯示用戶的當前位置,請使用MyLocationOverlay。它是專門爲此設計的。 Here是如何使用它的一個很好的例子,以下是基本的核心代碼片段:

// This goes into the onCreate method 
myLocationOverlay = new MyLocationOverlay(this, mapView); 
mapView.getOverlays().add(myLocationOverlay); 

myLocationOverlay.runOnFirstFix(new Runnable() { 
    public void run() { 
    mapView.getController().animateTo(myLocationOverlay.getMyLocation()); 
    } 
}); 

參見教程進行更詳細的例子。

+0

正如我需要出示其他標記的也對谷歌地圖,這樣才符合'ItemizedOverlay concept'我去的原因。首先,當我的應用程序啓動時,我需要專注於當前位置,始終使用'ItemizedOverlay'。我的例子的任何建議將不勝感激。 – ferhan 2012-08-12 08:51:06

+0

您可以同時使用MyLocationOverlay和其他疊加層。我沒有教程,但谷歌應該給你足夠的。 – 2012-08-12 09:04:08

0
GeoPoint point = new GeoPoint(lat, lng); 
change above code 

declare global variable 
GeoPoint point; 

then assign onLocationChanged() method 

point = new GeoPoint(lat, lng); 

按照非常簡單的例子鏈接

http://android-coding.blogspot.com/2011/06/using-itemizedoverlay-to-add-marker-on.html

+0

這正是我所追求的,但在我的案例中,我使用的是「當前位置」概念,並且在該教程中他們對這些值進行了硬編碼。 – ferhan 2012-08-12 09:04:49

+0

你可以用日誌記錄檢查你的當前位置是否可用 – 2012-08-12 09:07:12