实现功能,页面中点击按钮,获得当前位置,显示到编辑框。

首先AndroidManifest.xml 文件中添加下面文件,原因略


2、activity_main.xml文件中代码,代码是在项目中截取的,有些地方啰嗦

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
tools:context=".MainActivity" >

android:id="@+id/main_tab_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

android:id="@+id/textView1"
style="@style/titleSty"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center"
android:gravity="center_horizontal|center_vertical"
android:text="车辆基本信息" />

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f6f6f6" >

android:id="@+id/textView1"
style="@style/titleSty"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center"
android:gravity="center_horizontal|center_vertical"
android:text="获取当前位置" />

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f6f6f6" >

android:id="@+id/textView2"
style="@style/content_18_black"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="5dp"
android:text="位置信息" />

android:id="@+id/main_editText1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@id/textView2"
style="@style/content_18_black"
android:background="@drawable/editbackground"
>

android:layout_width="39dp"
android:layout_height="30dp"
android:layout_alignBottom="@+id/main_editText1"
android:layout_alignRight="@+id/main_editText1"
android:layout_alignTop="@+id/main_editText1"
android:layout_marginBottom="3dp"
android:layout_marginRight="6dp" >

android:id="@+id/main_position"
android:layout_width="36dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:background="@drawable/pop2" />


activity中代码:

package net.tycmc.bulb.androidstandard;

import net.tycmc.bulb.androidstandard.base.GetPosition;
import net.tycmc.bulb.androidstandard.base.ViewGroupSOK;
import net.tycmc.bulb.androidstandard.partlist.ui.PartListActivity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class MainActivity extends Activity {
private ImageButton imgButton;
private EditText position;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
position = (EditText) findViewById(R.id.main_editText1);
imgButton = (ImageButton) findViewById(R.id.main_position);

imgButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getPosition();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//获取位置信息
public void getPosition() {
// TODO Auto-generated method stub
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
30000, 50, mLocationListener01);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 30000, 50,
mLocationListener01);
}

// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
public final LocationListener mLocationListener01 = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateToNewLocation(location);
}

@Override
public void onProviderDisabled(String provider) {
updateToNewLocation(null);
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};

private Location updateToNewLocation(Location location) {
System.out.println("--------zhixing--2--------");
String latLongString;
double lat = 0;
double lng = 0;

if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
latLongString = "纬度:" + lat + "经度:" + lng;
System.out.println("经度:" + lng + "纬度:" + lat);
} else {
latLongString = "请稍等...";
}
if (lat != 0) {
System.out.println( String.valueOf(lat));
}
position.setText(latLongString);
return location;

}
}

效果图: