2010-02-23 141 views
1

我已经完成了创建一个向程序添加标记的android程序。 但我需要在MAP中显示CURRENT MY LOCATION。我知道这是通基于位置的服务做,但我不知道如何将其纳入我的计划获取当前位置

这里是我的onCreate命令


    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);

//map 
    mapView = (MapView) findViewById(R.id.mapView); 

    //zoom 
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
     new LinearLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true); 

    //Philippines coordinate 
    mc = mapView.getController(); 
    String coordinates[] = {"10.952566007", "121.78921587"}; 
    double lat = Double.parseDouble(coordinates[0]); 
    double lng = Double.parseDouble(coordinates[1]); 

    p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 

    mc.animateTo(p); 
    mc.setZoom(6); 

    mapOverlay = new MapOverlay(); 
    listOfOverlays = mapView.getOverlays(); 
    listOfOverlays.clear(); 
    listOfOverlays.add(mapOverlay); 

    mapView.invalidate(); 
} 

我有一个放置标记的的onTouchEvent 。所以没问题, 问题是我需要在程序开始时显示我的位置。它应该显示一个标记,上面写着“你在这里”或类似的东西。

p.s:我想我需要改变硬编码点到我目前的位置点,但我不知道该怎么做:

回答

0

Hello MapView显示如何放置叠加层。我想你想使用the google APIS reference中记录的MyLocationOverlay类来获取当前位置。

+0

另一个比你想要的更好的示例是anddev.org上的friend finder应用程序:http:/ /www.anddev.org/the_friend_finder_-_mapactivity_using_gps_-_part_i_-_ii-t93.html – RickNotFred 2010-02-23 13:39:41

2

为了让您的手机的当前位置,我用

的LocationManager的LocationManager =(的LocationManager)this.getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); String bestProvider = locationManager.getBestProvider(criteria,true); 位置loc = locationManager.getLastKnownLocation(bestProvider);

限定在AndroidManifest.xml用途-权限这些 (使用细位置其他的东西)

REF。 http://code.google.com/p/android-bluetooth-on-motion/source/browse/trunk/BluetoothOnMotion/src/com/elsewhat/android/onmotion/BluetoothOnMotionService.java

在我的应用程序中,我希望将参数传递给地图意图,以便地图显示标记,但是找不到方法。按照Rick(谁不​​是Fred)的建议创建位置叠加层可能是要走的路线

+0

当我将这段代码合并到我的程序中时,它返回一个空位置。 – lulala 2010-02-28 06:50:02

+0

很奇怪。您是否按照与我相同的方式设置了AndroidManifest.xml,请参阅http://code.google.com/p/android-bluetooth-on-motion/source/browse/trunk/BluetoothOnMotion/AndroidManifest.xml? – dparnas 2010-03-08 16:37:13