wifi 网络定位 方式

来源:互联网 发布:淘宝哪里可以领红包 编辑:程序博客网 时间:2024/04/28 11:50
package com.studio.android.chp08.ex01;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.location.Criteria;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class CurrentLocation extends Activity {    /** Called when the activity is first created. */  private Location local=null;  private Location localBackup=null;  private int i=0;   /**     * LocationListener 是一个接口     * new LocationListener() 去实例化这个接口     * new LocationListener() {}对接口的方法进行实现     *      * 整体而言是个 成员变量 复制语句,可以放到 前面去     * 这个成员变量 是 LocationListener 类型     */   private final LocationListener locationListener = new LocationListener() {    public void onLocationChanged(Location location) {    updateWithNewLocation(location);//地址改变    //local=location;    System.out.println("the onLocationChanged");    }    public void onProviderDisabled(String provider){    updateWithNewLocation(null);//地址失效    System.out.println("the onProviderDisabled");    }    public void onProviderEnabled(String provider){    System.out.println("the onProviderEnabled");        }        public void onStatusChanged(String provider, int status,Bundle extras){     System.out.println("the onStatusChanged");            }    };        public Location getLocal() {return local;}public void setLocal(Location local) {this.local = local;}@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                        setContentView(R.layout.main);  //选择布局,用 main 里面的布局方式                Button btv=(Button)findViewById(R.id.GoSecond);                LocationManager locationManager;        String serviceName = Context.LOCATION_SERVICE;        locationManager = (LocationManager)getSystemService(serviceName);      //  String provider = LocationManager.GPS_PROVIDER;                String provider2= LocationManager.NETWORK_PROVIDER;                             localBackup = locationManager.getLastKnownLocation(provider2);        if(localBackup!=null)        updateWithNewLocation(localBackup);               locationManager.requestLocationUpdates(provider2, 2000, 0,         locationListener);//注册了监听器                        btv.setOnClickListener(new View.OnClickListener(){public void onClick(View v) {// TODO 实现这个接口的方法Intent intent = new Intent();if(CurrentLocation.this.local!=null){double lati = CurrentLocation.this.local.getLatitude();double logti=CurrentLocation.this.local.getLongitude();intent.putExtra("Lati",lati);intent.putExtra("Lnti", logti);}                intent.setClass(CurrentLocation.this, LocationName.class);                   startActivity(intent);                         }                            });                         }           private void updateWithNewLocation(Location location) {        String latLongString;    TextView myLocationText;        myLocationText = (TextView)findViewById(R.id.myLocationText);    if (location != null) {    double lat = location.getLatitude();    double lng = location.getLongitude();        this.setLocal(location);//更新 本类的location    System.out.println(" the lat is "+lat+"  the lng is "+lng);        latLongString = "第  "+ ++i+" 次  更新    纬度是:" + lat + "\n经度:" + lng;        } else {    latLongString = "无法获取地理信息";    }        myLocationText.setText("您当前的位置是:\n" +latLongString);    }}

 

 

误差在1m

 


原创粉丝点击