获取当前位置 [英] get current location

查看:107
本文介绍了获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了创建一个向某个位置添加标记的android程序。
但我需要在MAP中显示CURRENT MY LOCATION。我知道这是通过基于位置的服务完成的,但我不知道如何将它合并到我的程序中。



这是我的onCreate命令
<$ p $
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);
查看zoomView = mapView.getZoomControls();

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

//菲律宾坐标
mc = mapView.getController();
字符串坐标[] = {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 =新的MapOverlay();
listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);

mapView.invalidate();
}



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



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

解决方案

为了获得手机的当前位置,我使用

< blockquote>

LocationManager locationManager =(LocationManager)this.getSystemService(LOCATION_SERVICE);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String bestProvider = locationManager.getBestProvider(criteria,true);
location loc = locationManager.getLastKnownLocation(bestProvider);

在AndroidManifest.xml中为这些$ b定义使用权限

$ b(使用其他地方的优惠地点)



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



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

I'm done creating an android program that adds a marker to a location. But I need to show CURRENT MY LOCATION in the MAP. I know this is done thru location based services but I don't know how to incorporate it into my program

Here is my onCreate command


  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();
}

I have an onTouchEvent that places the marker. So no problem there, the problem is I need to display my location at the start of the program. It should show a mark that says, "You are here" or something like that in the map.

p.s: I think I need to change the hardcoded point to my current location point, but i don't know how to do that :|

解决方案

In order to get the current location of your phone I use

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

define uses-permissions in the AndroidManifest.xml for these (use the fine location for other stuff)

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

In my application I would like to pass a paramter to the maps intent in order for maps to show a marker, but have found no way of doing it. Creating a location overlay as suggested by Rick (who is not Fred) is probably the way to go

这篇关于获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆