文字返回
載入jar包
修改Manifest.xml 之前漏了<Service />弄了超久=_=
儘量在手機上測試 不要再模擬機上測+_+ <!-- 漏了這個service 結果調試了超久 -->
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.baidu.location.service_v2.2" >
</action>
</intent-filter>
</service>
<!-- meta-data service需要寫在application中 -->
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="M6lBKiDFrBGCk8UyQl9hbkAI" />
<!-- 這個key是手機上的 雖然之前是先用這個keystore的SHA1申請的key再打包安裝的.
可以直接在手機上調試 不用打包再安裝。如果要在模擬機上調試 是用Eclipse對應的debug模式的key-->
代碼
public class MainActivity extends Activity {
private TextView resultText;
private Button button;
private LocationClient locationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initLocation();
locationClient.registerLocationListener(new MyLocationListener());//註冊
}
public class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {
StringBuffer sBuffer=new StringBuffer(256);
sBuffer.append("\n Latitude : "+location.getLatitude());
sBuffer.append("\n Longitude : "+location.getLongitude());
sBuffer.append("\n Error Code : ");//不是錯誤代碼的意思 ==!是返回代碼的意思
sBuffer.append(location.getLocType());
if (location.getLocType() == BDLocation.TypeGpsLocation){//GPS返回地理信息
sBuffer.append("\n Address From GPS : "+location.getAddrStr());
sBuffer.append("\n Province : "+location.getProvince());
sBuffer.append("\n City : "+location.getCity());
sBuffer.append("\n District : "+location.getDistrict());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){//NET返回地理信息
sBuffer.append("\n Address From NET : ");
sBuffer.append(location.getAddrStr());
//運營商信息
sBuffer.append("\n Operationers : ");
sBuffer.append(location.getOperators());
}
showResult(sBuffer.toString());
}
}
private void showResult(String str){
try{
if(resultText!=null){
resultText.setText(str);
}
}catch(Exception e){
e.printStackTrace();
}
}
private void init(){
resultText=(TextView)findViewById(R.id.resultText);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(button.getText().equals("開啓")){
button.setText("關閉");
locationClient.start();
}else if(button.getText().equals("關閉")){
button.setText("開啓");
locationClient.stop();
resultText.setText("");
}
}
});
locationClient=new LocationClient(getApplicationContext());
}
private void initLocation(){//設置LocationClient的Option
LocationMode locationMode=LocationMode.Hight_Accuracy;
String tempcoor="gcj02";
LocationClientOption option=new LocationClientOption();
option.setOpenGps(true);
option.setLocationMode(locationMode);
option.setCoorType(tempcoor);
option.setScanSpan(1000);
option.setIsNeedAddress(true);
locationClient.setLocOption(option);
}
@Override
protected void onStop(){
locationClient.stop();
super.onStop();
}
}
截圖
Error Type 結果如下: