我想使用GPS,但我的code使用的WiFi .. !!!为什么? [英] I want to use GPS but my code uses WiFi..!!! why?

查看:126
本文介绍了我想使用GPS,但我的code使用的WiFi .. !!!为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的code,返回我的当前位置的地址。但我可以,如果我的WiFi开启手动只得到这些值。但我想从无线对GPS进行切换,并从GPS我的地址,而不是。有人告诉我什么是这个code进行修改,这样做。以下是code是用定位我现在的地址。提前致谢。

 公共字符串myloc()
{
    标准=新标准();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(假);
    criteria.setBearingRequired(假);
    criteria.setCostAllowed(真正的);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    字符串商= locationManager.getBestProvider(标准,真正的);

    //更新与最后已知的图形用户界面
    位置位置= locationManager.getLastKnownLocation(供应商);

    如果(位置!= NULL)
    {
        双纬度= location.getLatitude();
        双LNG = location.getLongitude();
        latLongString =纬度:+纬度+\ nLong:+ LNG;

        地理codeR GC =新的地缘codeR(这一点,Locale.getDefault());
        尝试
        {
            名单<地址>地址= gc.getFromLocation(纬度,经度,1);
            StringBuilder的SB =新的StringBuilder();
            如果(addresses.size()大于0)
            {
              地址地址= addresses.get(0);
              。sb.append(address.getAddressLine(0))追加(\ N);
              。sb.append(address.getLocality())追加(\ N);
              。sb.append(address.getAdminArea())追加(\ N);
              sb.append(address.getCountryName());

            }
            addressString = sb.toString();
        }
        赶上(IOException异常E)
        {

        }
    }
    其他
    {
        latLongString =找不到位置;
    }

    返回addressString;
}
 

解决方案

这也许对你有帮助。

下面是MyLocation.Java文件

 进口java.util.Timer中;
进口java.util.TimerTask中;
进口android.content.Context;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;

公共类MyLocation {
    定时器定时器1;
    LocationManager流明;
    LocationResult locationResult;
    布尔gps_enabled = FALSE;
    布尔network_enabled = FALSE;

    公共布尔的getLocation(上下文的背景下,LocationResult结果)
    {
        //我用LocationResult回调类从MyLocation用户code传递位置值。
        locationResult =结果;
        如果(LM == NULL)
            LM =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

        //如果供应商是不允许的异常将被抛出。
        尝试{gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}赶上(例外前){}
        尝试{network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}赶上(例外前){}

        //如果没有提供启动不启动监听器
        如果(gps_enabled&安培;!&安培;!network_enabled)
            返回false;

        如果(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListenerGps);
        如果(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListenerNetwork);
        定时器1 =新的Timer();
        timer1.schedule(新GetLastLocation(),60000);
        返回true;
    }

    LocationListener的locationListenerGps =新LocationListener的(){
        公共无效onLocationChanged(位置定位){
            timer1.cancel();
            locationResult.gotLocation(位置);
            lm.removeUpdates(本);
            lm.removeUpdates(locationListenerNetwork);
        }
        公共无效onProviderDisabled(字符串提供商){}
        公共无效onProviderEnabled(字符串提供商){}
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}
    };

    LocationListener的locationListenerNetwork =新LocationListener的(){
        公共无效onLocationChanged(位置定位){
            timer1.cancel();
            locationResult.gotLocation(位置);
            lm.removeUpdates(本);
            lm.removeUpdates(locationListenerGps);
        }
        公共无效onProviderDisabled(字符串提供商){}
        公共无效onProviderEnabled(字符串提供商){}
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}
    };

    类GetLastLocation扩展的TimerTask {
        @覆盖
        公共无效的run(){
             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             位置net_loc = NULL,gps_loc = NULL;
             如果(gps_enabled)
                 gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             如果(network_enabled)
                 net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

             //如果有两个值,用最新一期
             如果(gps_loc = NULL和放大器;!&安培;!net_loc = NULL){
                 如果(gps_loc.getTime()> net_loc.getTime())
                     locationResult.gotLocation(gps_loc);
                 其他
                     locationResult.gotLocation(net_loc);
                 返回;
             }

             如果(gps_loc!= NULL){
                 locationResult.gotLocation(gps_loc);
                 返回;
             }
             如果(net_loc!= NULL){
                 locationResult.gotLocation(net_loc);
                 返回;
             }
             locationResult.gotLocation(空);
        }
    }

    公共静态抽象类LocationResult {
        公共抽象无效gotLocation(位置定位);
    }
}
 


现在,在另一个主要的类文件中使用此文件

  MyLocation myLocation =新MyLocation();

@覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.splash);
        findCurrentLocation();

    }

私人无效findCurrentLocation(){
        myLocation.getLocation(这一点,locationResult);
    }

公共LocationResult locationResult =新LocationResult(){

        @覆盖
        公共无效gotLocation(位置定位){
            // TODO自动生成方法存根
            如果(位置!= NULL){
                串strloc = location.getLatitude()+,
                        + location.getLongitude();
            }
        }
    };
 

strloc现在吐司的messge。

谢谢, 聂...

I have written the following code that returns my current location address. But i can get these values only if the my Wifi is switched on manually. but i want to switch on the Gps and get my address from GPS and not from Wifi. Someone please tell me what is to be modified in this code to do so. Following is the code am using to locate my current address. Thanks in advance.

public String myloc()
{ 
    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    String provider = locationManager.getBestProvider(criteria, true);

    // Update the GUI with the last known 
    Location location = locationManager.getLastKnownLocation(provider);

    if(location!=null)
    {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        latLongString = "Lat:" + lat + "\nLong:" + lng;

        Geocoder gc = new Geocoder(this, Locale.getDefault());
        try
        {
            List<Address> addresses = gc.getFromLocation(lat, lng, 1);
            StringBuilder sb = new StringBuilder();
            if (addresses.size() > 0) 
            {
              Address address = addresses.get(0);
              sb.append(address.getAddressLine(0)).append("\n");
              sb.append(address.getLocality()).append("\n");
              sb.append(address.getAdminArea()).append("\n");
              sb.append(address.getCountryName());

            }
            addressString = sb.toString();
        } 
        catch (IOException e)
        {

        }
    } 
    else 
    {
        latLongString = "No location found";
    }

    return addressString;
}

解决方案

It may be helpful to you.

Below is MyLocation.Java file

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled=false;
    boolean network_enabled=false;

    public boolean getLocation(Context context, LocationResult result)
    {
        //I use LocationResult callback class to pass location value from MyLocation to user code.
        locationResult=result;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            return false;

        if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 60000);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             Location net_loc=null, gps_loc=null;
             if(gps_enabled)
                 gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             if(network_enabled)
                 net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

             //if there are both values use the latest one
             if(gps_loc!=null && net_loc!=null){
                 if(gps_loc.getTime()>net_loc.getTime())
                     locationResult.gotLocation(gps_loc);
                 else
                     locationResult.gotLocation(net_loc);
                 return;
             }

             if(gps_loc!=null){
                 locationResult.gotLocation(gps_loc);
                 return;
             }
             if(net_loc!=null){
                 locationResult.gotLocation(net_loc);
                 return;
             }
             locationResult.gotLocation(null);
        }
    }

    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}


And now used this file in another Main class file

MyLocation myLocation = new MyLocation();

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        findCurrentLocation();

    }

private void findCurrentLocation() {
        myLocation.getLocation(this, locationResult);
    }

public LocationResult locationResult = new LocationResult() {

        @Override
        public void gotLocation(Location location) {
            // TODO Auto-generated method stub
            if (location != null) {
                String strloc  = location.getLatitude() + ","
                        + location.getLongitude();
            }
        }
    };

now Toast messge of "strloc".

Thanks, Nik...

这篇关于我想使用GPS,但我的code使用的WiFi .. !!!为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆