项目初步想法:
当父母主动要求定位时,儿童端发送其定位信息及地址信息给父母,并且父母可以根据得到的经纬度查询其周围的地图。
最好的资料:百度地图的API文档。
http://developer.baidu.com/map/index.php?title=android-locsdk
在这个链接里面,详细地介绍了KEY申请、权限声明、类的导入等以及具体的环境搭建。
在比较好的教程中,可以参考这两个教程:
根据经纬度查询地址信息
根据经纬度获取地图:
获取经纬度的教程:
参见百度API文档
2.具体的程序实现:
在MainActivity中主要起着显示布局以及监控控件的作用,将具体的实现程序放在另外一个java文件中来实现,这样有利于mainActivity.java文件的整洁性和主控性。
在main文件中监听控件触发行为的方法:
mTv = (TextView)findViewById(R.id.textview);
mLocClient = ((Location)getApplication()).mLocationClient;
((Location)getApplication()).mTv = mTv;
mVibrator01 =(Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
((Location)getApplication()).mVibrator01 = mVibrator01;
//开始按钮 ,具体设置要看API文档
//由于SDK版本不同,导致网上的 很多教程不可以用,最好用最新版本的教程,并且根据百度文档一步步配置,是完 全可以做出来的,不要去照抄别人的代码。
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
option.setIsNeedAddress(true);//返回的定位结果包含地址信息 ,记得包含上。
mLocClient.setLocOption(option); //让这些设置生效。
btnStart.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
setLocationOption();
mLocClient.start();
}
在Location.java文件中:
public class Location extends Application {
private String PHONENUM="13710172704";
@Override
public void onCreate() {
mLocationClient = new LocationClient( this );
mLocationClient.registerLocationListener( myListener );
super.onCreate();
}
public void logMsg(String str) {
try {
mData = str;
if ( mTv != null )
{
mTv.setText(mData);
msg = "妈妈,我有危险!我在"+ mData;
String cnum = PHONENUM;
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(cnum, null, msg, null, null);
// 提示短信发送完成
Toast.makeText(this, "短信发送完成", 10000).show();
// Log.i("tag", "msgs");
}
// public void sendmsg() {
// Log.i("tag", "msgs");}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
// * 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return ;
StringBuffer sb = new StringBuffer(256);
sb.append("\n北纬 : ");
sb.append(location.getLatitude());
sb.append("\n精度 : ");
sb.append(location.getLongitude());
if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\n地址 : ");
sb.append(location.getAddrStr());
/* sb.append("\n省份 : ");
sb.append(location.getProvince());
sb.append("\n城市 : ");
sb.append(location.getCity());
sb.append("\n区县 : ");
sb.append(location.getDistrict());*/
}
// sb.append("\nradius : ");
// sb.append(location.getRadius());
logMsg(sb.toString());
}
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null){
return ;
}
StringBuffer sb = new StringBuffer(256);
sb.append("\nlatitude : ");
sb.append(poiLocation.getLatitude());
sb.append("\nlontitude : ");
sb.append(poiLocation.getLongitude());
if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\n地址 : ");
sb.append(location.getAddrStr());
}
// sb.append("\nradius : ");
//sb.append(poiLocation.getRadius());
}
}
public class NotifyLister extends BDNotifyListener{
public void onNotify(BDLocation mlocation, float distance){
mVibrator01.vibrate(1000);
}
}
}
3。根据输入的经纬度查询地图:
queryButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// 将用户输入的经纬度值转换成int类型
// 用户输入的经度值
longitudeStr = longitudeEditText.getText().toString();
// 用户输入的纬度值
latitudeStr = latitudeEditText.getText().toString();
Intent intent = new Intent();
intent.putExtra("latitudeStr", latitudeStr);//让intent直接装进2个字符串,而不是搭上2个背包。
intent.putExtra("longitudeStr", longitudeStr);
//注意到这里由第一个activity传给第二个activity两个字符串
Bundle b=new Bundle();
b.putStringArray("pose_title", pose_title);
intent.putExtras(b);
intent.setClass(MainActivity.this, SecondActivity.class);
MainActivity.this.startActivity(intent);
// 创建Intent对象
/* Intent intent = new Intent();
// 为Intent设置Action属性(属性值就是一个普通字符串)
intent.setAction(MainActivity.CRAZYIT_ACTION);
Bundle bundle = new Bundle();
startActivity(intent);*/
} catch (Exception e) {
addressTextView.setText("查询出错,请检查您输入的经纬度值!");
}
}
});
在第二个activity中对intent的数据进行处理:
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e("gwg1"," cd");
super.onCreate(savedInstanceState);
//初始化BMapManager对象(注意:请在试用setContentView前初始化,否则会报错)
mBMapMan=new BMapManager(getApplication());
mBMapMan.init("1C3CFC48C3C6F37C2E546FC57410E1D9FB3EF07E", null);
setContentView(R.layout.second);
Log.e("gwg1"," cd");
mMapView=(MapView)findViewById(R.id.mapview);
mMapView.setBuiltInZoomControls(true);
Intent intent = getIntent();
String bsString = intent.getStringExtra("latitudeStr");
String asString = intent.getStringExtra("longitudeStr");
Bundle b=this.getIntent().getExtras();
pose_title = b.getStringArray("pose_title");
Log.i("gwg3",bsString);
int latitude = (int) (1000000 * Double.parseDouble(bsString));
Log.d("gwg2"," de");
int longitude = (int) (1000000 * Double.parseDouble(asString));
Log.e("gwg1"," cd");
GeoPoint point =new GeoPoint((int)(latitude),(int)(longitude));
//设置启用内置的缩放控件
MapController mMapController=mMapView.getController();
// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
// AActivity,您辛苦了,卸下你的背包吧,看看你给我带来了什么好消息。
Log.i("gwg4"," fd");
//用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
mMapController.setCenter(point);//设置地图中心点
Log.e("gwg1"," cd");
mMapController.setZoom(12);//设置地图zoom级别
这里用到了很多个log.e是为了在控制台上看出它运行到的位置。
如果只是由第一个activity传给第二个activity一个数组,可以构建bundle的方法进行传输;
知道android中两个Activity之间如何传递字符串的吗? 出发地: AActivity, 目的地:BActivity。 执行任务:把字符串strTrans传递过去。 开始 -> 出发地:AActivity
Intent intAAA = new Intent();
intAAA.setClass(AActivity.this, BActivity.class);
Bundle bundle = new Bundle();
bundle.putString("TransWords", strTrans); // strTrans为要传递的字符串
intAAA.putExtras(bundle); // intAAA出发前,背上Bundle背包,别忘记,背包里有strTrans字符串呢
startActivity(intAAA); // 出发喽!
目的地:BActivity // AActivity,您辛苦了,卸下你的背包吧,看看你给我带来了什么好消息。 Bundle bundle = this.getIntent().getExtras(); //打开A带来的背包
String strT =bundle.getString("strTrans"); //取出字符串strTrans
o,my 嘎,你给我带来最令我兴奋的消息 strTrans...
|
/*靠人不如靠自己,求人不易,冷暖自知。*/