标签:
刚开始试着去做一个百度地图定位实验,感觉挺好玩的,做起来也十分简单,而且最主要还是挺实用。
先上效果图:
百度api:http://lbsyun.baidu.com/index.php?title=android-locsdk
按照百度上边的步骤,申请密钥,配置环境
最后上代码:
package com.example.testlocation;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.a.f;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "dzt";
private TextView mText;
private TextView mTextPoi;
private LocationClient mLocationClient = null;
private BDLocationListener myListener = new MyLocationListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationClient = new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(myListener);
setLocationOption();
mLocationClient.start();
initWidgets();
}
private void setLocationOption() {
// TODO Auto-generated method stub
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setIsNeedAddress(true);
option.setAddrType("all");
option.setCoorType("bd0911");
option.setScanSpan(5000);
option.disableCache(true);
mLocationClient.setLocOption(option);
}
private void initWidgets() {
// TODO Auto-generated method stub
mText = (TextView)findViewById(R.id.textView2);
// mTextPoi = (TextView)findViewById(R.id.textView3);
Button btn = (Button)findViewById(R.id.button1);
Button btn2 = (Button)findViewById(R.id.Send);
btn.setOnClickListener(this);
btn = (Button)findViewById(R.id.button2);
btn.setOnClickListener(this);
btn2.setOnClickListener(this);
}
protected void onDestroy(){
super.onDestroy();
mLocationClient.stop();
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
if (mLocationClient != null && mLocationClient.isStarted())
mLocationClient.requestLocation();
else
Log.d(TAG, "locClient is null or not started");
break;
case R.id.button2:
// 请求POI数据
// if (mLocationClient != null && mLocationClient.isStarted())
mText.setText(null);
break;
case R.id.Send:
Log.i("Send SMS", "");
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address" , new String ("13763304516"));
smsIntent.putExtra("sms_body" , mText.getText().toString());
try {
startActivity(smsIntent);
finish();
Log.i("Finished sending SMS...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
}
default:
break;
}
}
public class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (location == null)
return;
StringBuffer sb = new StringBuffer();
sb.append("Current Time: ");
sb.append(location.getTime());
sb.append(location.getTime());
sb.append("\n错误码 : ");
sb.append(location.getLocType());
sb.append("\n纬度 : ");
sb.append(location.getLatitude());
sb.append("\n经度 : ");
sb.append(location.getLongitude());
sb.append("\n半径 : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\n速度 : ");
sb.append(location.getSpeed());
sb.append("\n卫星数 : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\n地址 : ");
sb.append(location.getAddrStr());
}
mText.setText(sb.toString());
Log.d(TAG, "onReceiveLocation " + sb.toString());
}
public void onReceivePoi(BDLocation poiLocation) {
// 将在下个版本中去除poi功能
if (poiLocation == null) {
return;
}
StringBuffer sb = new StringBuffer(256);
sb.append("Poi time : ");
sb.append(poiLocation.getTime());
sb.append("\nerror code : ");
sb.append(poiLocation.getLocType());
sb.append("\nlatitude : ");
sb.append(poiLocation.getLatitude());
sb.append("\nlontitude : ");
sb.append(poiLocation.getLongitude());
sb.append("\nradius : ");
sb.append(poiLocation.getRadius());
if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\naddr : ");
sb.append(poiLocation.getAddrStr());
}
mTextPoi.setText(sb.toString());
Log.d(TAG, "onReceivePoi " + sb.toString());
}
}
}
标签:
原文地址:http://www.cnblogs.com/androidxxx/p/5319569.html