因为最近用到定位获取城市天气的功能,公司提供的天气的接口,特此做了一下定位的功能。处女作,有不好之处还请博友们多多指教。
以下是xml里面的代码,仅仅有一个按钮跟一个文本框,按钮用来点击,文本框用来显示
<Button
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="定位"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#cc00ff"
android:layout_marginLeft="20dip"
android:layout_toRightOf="@id/location"
android:gravity="center"
android:textSize="30sp" />
接下来的需要用到的权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
应用里面涉及到对AndroidManifest.xml文件里的application中加一段代码(颜色标注)
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="9806db1ffe65f03908039f7be91b61b9" />
<activity
android:name="com.example.locationdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
最后是activity里的代码
实现AMapLocationListener接口,系统会提示一些重写的方法,此处只在一个方法里面写就成
@Override
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
if (location != null) {
String str =arg0.getCity().split("市")[0];
tvLocation.setText(str);
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
tvLocation就是我们上面提到的文本框,里面显示的str即我们所要的城市名。
一下是activity的全部代码
public class MainActivity extends Activity implements OnClickListener,AMapLocationListener {
private TextView tvLocation;
private Button location;
private LocationManagerProxy mAMapLocManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
location = (Button) findViewById(R.id.location);
location.setOnClickListener(this);
tvLocation = (TextView) findViewById(R.id.tv_location);
}
@Override
public void onClick(View arg0) {
if(arg0 == location){
mAMapLocManager = LocationManagerProxy.getInstance(MainActivity.this);
mAMapLocManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork, 5000, 10, MainActivity.this);
tvLocation.setText("");
}
}
@Override
protected void onPause() {
super.onPause();
if (mAMapLocManager != null) {
mAMapLocManager.removeUpdates(this);
}
}
@Override
protected void onDestroy() {
if (mAMapLocManager != null) {
mAMapLocManager.removeUpdates(this);
mAMapLocManager.destory();
}
mAMapLocManager = null;
super.onDestroy();
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
if (location != null) {
String str =arg0.getCity().split("市")[0];
tvLocation.setText(str);
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
}
最后要提醒大家一点,需要架包!!!
包名Android_Location_V1.0.4.jar,大家从网上搜来下载吧。
完工!