wifi 网络定位 方式

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

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值