标签:
1 package com.example.zhangle02; 2 3 import com.baidu.location.BDLocation; 4 import com.baidu.location.BDLocationListener; 5 import com.baidu.location.LocationClient; 6 import com.nostra13.universalimageloader.core.ImageLoader; 7 import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 8 9 import android.app.Application; 10 import android.util.Log; 11 import android.widget.TextView; 12 13 public class LocationApplication extends Application { 14 15 public LocationClient mLocationClient;//定位SDK的核心类 16 public MyLocationListener mMyLocationListener;//定义监听类 17 public TextView mLocationResult,logMsg; 18 @Override 19 public void onCreate() { 20 super.onCreate(); 21 ImageLoaderConfiguration configuration = ImageLoaderConfiguration 22 .createDefault(this); 23 24 //Initialize ImageLoader with configuration. 25 ImageLoader.getInstance().init(configuration); 26 27 28 29 mLocationClient = new LocationClient(this.getApplicationContext()); 30 mMyLocationListener = new MyLocationListener(); 31 mLocationClient.registerLocationListener(mMyLocationListener); 32 } 33 34 /** 35 * 实现实位回调监听 36 */ 37 public class MyLocationListener implements BDLocationListener { 38 39 public void onReceiveLocation(BDLocation location) { 40 //Receive Location 41 StringBuffer sb = new StringBuffer(256); 42 // sb.append("time : "); 43 // sb.append(location.getTime());//获得当前时间 44 // sb.append("\nerror code : "); 45 // sb.append(location.getLocType());//获得erro code得知定位现状 46 // sb.append("\nlatitude : "); 47 // sb.append(location.getLatitude());//获得纬度 48 // sb.append("\nlontitude : "); 49 // sb.append(location.getLongitude());//获得经度 50 // sb.append("\nradius : "); 51 // sb.append(location.getRadius()); 52 if (location.getLocType() == BDLocation.TypeGpsLocation){//通过GPS定位 53 // sb.append("\nspeed : "); 54 // sb.append(location.getSpeed());//获得速度 55 // sb.append("\nsatellite : "); 56 // sb.append(location.getSatelliteNumber()); 57 // sb.append("\ndirection : "); 58 // sb.append("\naddr : "); 59 // sb.append(location.getAddrStr());//获得当前地址 60 // sb.append(location.getDirection());//获得方位 61 62 } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){//通过网络连接定位 63 // sb.append("\naddr : "); 64 sb.append(location.getAddrStr().substring(2, 5));//获得当前地址 65 //运营商信息 66 // sb.append("\noperationers : "); 67 // sb.append(location.getOperators());//获得经营商? 68 } 69 logMsg(sb.toString()); 70 Log.i("BaiduLocationApiDem", sb.toString()); 71 } 72 } 73 74 /** 75 * 显示请求字符串 76 * @param str 77 */ 78 public void logMsg(String str) { 79 try { 80 if (mLocationResult != null) 81 mLocationResult.setText(str); 82 } catch (Exception e) { 83 e.printStackTrace(); 84 } 85 } 86 87 }
1 package com.example.zhangle02; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.baidu.location.LocationClient; 7 import com.baidu.location.LocationClientOption; 8 import com.baidu.location.LocationClientOption.LocationMode; 9 10 import android.app.Activity; 11 import android.content.Intent; 12 import android.os.Bundle; 13 import android.view.View; 14 import android.widget.AdapterView; 15 import android.widget.AdapterView.OnItemClickListener; 16 import android.widget.ArrayAdapter; 17 import android.widget.ListView; 18 import android.widget.TextView; 19 20 public class MaiN extends Activity { 21 private LocationClient mLocationClient;//定位SDK的核心类 22 TextView city,onclik; 23 ListView lv; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.main); 29 city=(TextView) findViewById(R.id.gps_city); 30 onclik=(TextView) findViewById(R.id.textView1); 31 lv=(ListView) findViewById(R.id.lv1); 32 logicCode(); 33 } 34 35 private void logicCode() { 36 // TODO Auto-generated method stub 37 mLocationClient = ((LocationApplication)getApplication()).mLocationClient; 38 ((LocationApplication)getApplication()).mLocationResult = city;//调用LocationApplication,获得需要的信息 39 // onclik.setOnClickListener(new OnClickListener() { 40 // 41 // public void onClick(View v) { 42 InitLocation(); 43 44 mLocationClient.start(); 45 46 // 47 // } 48 // }); 49 50 51 List<String> list=new ArrayList<String>(); 52 list.add("上海"); 53 list.add("天津"); 54 list.add("深圳"); 55 list.add("衡水"); 56 list.add("香港"); 57 list.add("香1港"); 58 59 lv.setAdapter(new ArrayAdapter<String>(MaiN.this, android.R.layout.simple_list_item_1, list)); 60 61 lv.setOnItemClickListener(new OnItemClickListener() { 62 63 @Override 64 public void onItemClick(AdapterView<?> parent, View view, 65 int position, long id) { 66 Intent in=new Intent(MaiN.this,MainActivity.class); 67 startActivity(in); 68 69 } 70 }); 71 72 73 } 74 75 private void InitLocation(){ 76 LocationClientOption option = new LocationClientOption(); 77 option.setLocationMode(LocationMode.Hight_Accuracy);//设置高精度定位定位模式 78 option.setCoorType("bd09ll");//设置百度经纬度坐标系格式 79 option.setScanSpan(1000);//设置发起定位请求的间隔时间为1000ms 80 option.setIsNeedAddress(true);//反编译获得具体位置,只有网络定位才可以 81 mLocationClient.setLocOption(option); 82 } 83 84 85 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 8 <RelativeLayout 9 android:layout_height="50dp" 10 android:layout_width="match_parent" 11 12 > 13 14 <TextView 15 android:id="@+id/textView1" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_alignParentLeft="true" 19 android:layout_centerVertical="true" 20 android:text="GPS城市定位" 21 android:textAppearance="?android:attr/textAppearanceMedium" /> 22 23 </RelativeLayout> 24 25 <RelativeLayout 26 android:layout_height="50dp" 27 android:layout_width="match_parent" 28 29 > 30 31 <TextView 32 android:id="@+id/gps_city" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:layout_alignParentLeft="true" 36 android:layout_centerVertical="true" 37 android:text="城市定位" 38 android:textAppearance="?android:attr/textAppearanceMedium" /> 39 40 </RelativeLayout> 41 42 <RelativeLayout 43 android:layout_width="match_parent" 44 android:layout_height="50dp" > 45 46 <TextView 47 android:id="@+id/textView2" 48 android:layout_width="wrap_content" 49 android:layout_height="wrap_content" 50 android:layout_alignParentLeft="true" 51 android:layout_centerVertical="true" 52 android:text="热门城市" 53 android:textAppearance="?android:attr/textAppearanceMedium" /> 54 </RelativeLayout> 55 56 <ListView 57 android:id="@+id/lv1" 58 android:layout_height="match_parent" 59 android:layout_width="match_parent" 60 ></ListView> 61 62 </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/zle-mr/p/5551248.html