package com.gu.ALocationByGD;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
/**
* 本实例采用高德的智能定位包进行的智能选择定位方式进行定位,wifi网络、周围基站以及gps
*/
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.location).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开始定位
initLocation();
}
});
}
//定位管理服务
private LocationManagerProxy mLocationManagerProxy;
//停止定位,在定位完不需要再次定位是关闭定位服务,释放资源
private void stopLocation() {
if (mLocationManagerProxy != null) {
mLocationManagerProxy.removeUpdates(amapLocationListener);
mLocationManagerProxy.destory();
}
mLocationManagerProxy = null;
}
//初始化定位
private void initLocation() {
mLocationManagerProxy = LocationManagerProxy.getInstance(this);
//此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
//注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
//在定位结束后,在合适的生命周期调用destroy()方法
//其中如果间隔时间为-1,则定位只定一次
mLocationManagerProxy.requestLocationUpdates(LocationProviderProxy.AMapNetwork, -1, 1, amapLocationListener);
mLocationManagerProxy.setGpsEnable(false);
}
//定位监听
private AMapLocationListener amapLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
saveDisconnectLocation(aMapLocation);
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
double lat, lon;
//保存定位到的坐标位置,并跳转到地图界面进行定位
private void saveDisconnectLocation(AMapLocation amaplocation) {
lon = amaplocation.getLongitude();
lat = amaplocation.getLatitude();
// disconnect record
if (lon != 0 && lat != 0) {
stopLocation();
Intent intent = new Intent();
intent.setClass(MyActivity.this, MapActivity.class);
intent.putExtra("lat", lat);
intent.putExtra("lon", lon);
startActivity(intent);
finish();
}
}
}
资源文件列表
ALocationByGD/ALocationByGD.iml , 1015
ALocationByGD/AndroidManifest.xml , 1908
ALocationByGD/ant.properties , 698
ALocationByGD/build.xml , 4004
ALocationByGD/gen/com/gu/ALocationByGD/BuildConfig.java , 262
ALocationByGD/gen/com/gu/ALocationByGD/Manifest.java , 190
ALocationByGD/gen/com/gu/ALocationByGD/R.java , 176
ALocationByGD/libs/AMap_2DMap_V2.3.0.jar , 390352
ALocationByGD/libs/AMap_Services_V2.3.0.jar , 219079
ALocationByGD/libs/Android_Location_V1.3.0.jar , 182980
ALocationByGD/local.properties , 410
ALocationByGD/proguard-project.txt , 801
ALocationByGD/project.properties , 563
ALocationByGD/res/drawable-hdpi/ic_launcher.png , 9397
ALocationByGD/res/drawable-ldpi/ic_launcher.png , 2729
ALocationByGD/res/drawable-mdpi/ic_launcher.png , 5237
ALocationByGD/res/drawable-xhdpi/ic_launcher.png , 14383
ALocationByGD/res/layout/main.xml , 849
ALocationByGD/res/layout/map_layout.xml , 242
ALocationByGD/res/values-zh-rCN/strings.xml , 237
ALocationByGD/res/values-zh-rTW/strings.xml , 237
ALocationByGD/res/values/strings.xml , 237
ALocationByGD/src/com/gu/ALocationByGD/MapActivity.java , 6701
ALocationByGD/src/com/gu/ALocationByGD/MyActivity.java , 3533