700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android 虚拟基站和经纬度 android基于Gps 定位跟基站定位获取经纬度

android 虚拟基站和经纬度 android基于Gps 定位跟基站定位获取经纬度

时间:2023-07-13 07:23:14

相关推荐

android 虚拟基站和经纬度 android基于Gps 定位跟基站定位获取经纬度

一:新建MyLocationManager.java类,本类是为了代码架构方便把地位经纬度的代码在这类中实现然后通过回调方法,在activity中显示;

import android.content.Context;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

public class MyLocationManager {

private static Context mContext;

private LocationManager gpsLocationManager;

private LocationManager networkLocationManager;

private static final int MINTIME = 2000;

private static final int MININSTANCE = 2;

private static MyLocationManager instance;

private Location lastLocation = null;

private static LocationCallBack mCallback;

public static void init(Context c, LocationCallBack callback) {

mContext = c;

mCallback = callback;

}

private MyLocationManager() {

// Gps 定位

gpsLocationManager = (LocationManager) mContext

.getSystemService(Context.LOCATION_SERVICE);

Location gpsLocation = gpsLocationManager

.getLastKnownLocation(LocationManager.GPS_PROVIDER);

gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,

MINTIME, MININSTANCE, locationListener);

// 基站定位

networkLocationManager = (LocationManager) mContext

.getSystemService(Context.LOCATION_SERVICE);

Location networkLocation = gpsLocationManager

.getLastKnownLocation(LocationManager.GPS_PROVIDER);

networkLocationManager.requestLocationUpdates(

WORK_PROVIDER, MINTIME, MININSTANCE,

locationListener);

}

//单例模式实例化类

public static MyLocationManager getInstance() {

if (null == instance) {

instance = new MyLocationManager();

}

return instance;

}

private void updateLocation(Location location) {

lastLocation = location;

mCallback.onCurrentLocation(location);

}

private final LocationListener locationListener = new LocationListener() {

public void onStatusChanged(String provider, int status, Bundle extras) {

}

public void onProviderEnabled(String provider) {

}

public void onProviderDisabled(String provider) {

}

public void onLocationChanged(Location location) {

updateLocation(location);

}

};

public Location getMyLocation() {

return lastLocation;

}

private static int ENOUGH_LONG = 1000 * 60;

public interface LocationCallBack {

/**

* 当前位置

*

* @param location

*/

void onCurrentLocation(Location location);

}

//注销注册监听

public void destoryLocationManager() {

gpsLocationManager.removeUpdates(locationListener);

networkLocationManager.removeUpdates(locationListener);

}

}

二:在LocationActivity中实现LocationCallBack接口,如下:

import android.app.Activity;

import android.location.Location;

import android.os.Bundle;

import android.widget.TextView;

import com.android.fzmap.R;

import com.android.location.MyLocationManager.LocationCallBack;

public class LocationActivity extends Activity implements LocationCallBack {

private TextView desText;

private MyLocationManager mLocation;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

desText = (TextView) this.findViewById(R.id.text);

MyLocationManager.init(LocationActivity.this.getApplicationContext(),

LocationActivity.this);//初始化

mLocation = MyLocationManager.getInstance();//获取实例

}

//回调定位信息

public void onCurrentLocation(Location location) {

if (location != null) {

// 显示定位结果

desText.setText("当前经度:" + location.getLongitude() + "\n当前纬度:"

+ location.getLatitude());

}

}

// 关闭程序也关闭定位

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

mLocation.destoryLocationManager();

}

}

三:在AndroidManifest.xml中不要忘了要添加访问网络和启动定位等的几个权限

四:效果如下图:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。