码迷,mamicode.com
首页 > 其他好文 > 详细

LocationManager获取地理经纬度

时间:2015-04-20 18:35:19      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:android开发   定位   

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/show_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/get_btn"
        android:text="我要经纬度" />

</RelativeLayout>

-------------------代码-------------------------

public class MainActivity extends Activity implements OnClickListener {
/**显示经纬度的TextView*/
private TextView localTv = null;
/**操作Button*/
private Button getBtn = null;
/**定位管理器*/
private LocationManager mLocationManager;
/**经度*/
private double latitude = 0;
/**纬度*/
private double longitude = 0;
@SuppressLint("HandlerLeak")
private Handler localHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
double[] mess = (double[]) msg.obj;
localTv.setText("您所在位置经度为:" + mess[0] + "\t您所在位置纬度为:" + mess[1]);
}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localTv = (TextView) findViewById(R.id.show_tv);
getBtn = (Button) findViewById(R.id.get_btn);
getBtn.setOnClickListener(this);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
public void getLocalInfo() {
new Thread() {
@Override
public void run() {
Location location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
/**经度*/
latitude = location.getLatitude();
/**纬度*/
longitude = location.getLongitude();
/**谢谢到message中*/
double[] data = { latitude, longitude };
Message msg = localHandler.obtainMessage();
msg.obj = data;
localHandler.sendMessage(msg);
}
}
}.start();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.get_btn) {
getLocalInfo();
}
}
}

LocationManager获取地理经纬度

标签:android开发   定位   

原文地址:http://blog.csdn.net/true100/article/details/45152535

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!