最近在做一个关于地理位置的应用,但是现在需要已进入应用就打开gps,在网上找了些资料貌似都无法做到,来CSDN求助高手啊

解决方案 »

  1.   

    你可以看看这个,希望对你有所帮助
    http://groups.google.com/group/android-developers/browse_thread/thread/fec09ede096238e8/2738ff50f76cc739?lnk=gst&q=gps#2738ff50f76cc739
      

  2.   


    private final LocationListener locationListener = new LocationListener() {  
        public void onLocationChanged(Location location) { //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发  
            // log it when the location changes  
            if (location != null) {   
    upload.setEnabled(true);
    lat = location.getLatitude();
    lon = location.getLongitude();
    myCount.cancel();
            }  
        }  
      
        public void onProviderDisabled(String provider) {  
        // Provider被disable时触发此函数,比如GPS被关闭  
        }  
      
        public void onProviderEnabled(String provider) {  
        //  Provider被enable时触发此函数,比如GPS被打开
        }  
      
        public void onStatusChanged(String provider, int status, Bundle extras) {  
        // Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数  
        }  
    };  

    public Location getLocation(Context context) {
    LocationManager locMan = (LocationManager) context
    .getSystemService(Context.LOCATION_SERVICE);

    locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,  
     1000, 10, locationListener);
    locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,  
     1000, 10, locationListener);

    Location location = locMan
    .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location == null) {
    location = locMan
    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }
    return location;
    }
    在onCreate()里面这样调用:
    Location mLocation = getLocation(this);
    if (mLocation == null) {
    // position.setText("无法获取当前地理信息!");//TextView position
    ;
    } else {
    // position.setText("纬度:" + mLocation.getLongitude() + "\n经度:"
    // + mLocation.getLatitude());
    ;
    }这段代码 只是 GPS的 基站定位没这么简单 这段代码只能在室外使用
      

  3.   

    那继续求教基站定位的,另外这个不能实现在代码中打开GPS的功能
      

  4.   

    哦理解错了
    我认为GPS是否要打开是用户做决定的 而不是应用程序该有的权利
      

  5.   

    因为我们这个程序,需要快速的精确定位所以就,那像目前市场的LBS 中是如何在不启用gps的情况下进行精确定位的呢,不知道兄弟是否有详细的参考资料在地理位置这块
      

  6.   

    你可以试一下 把手机上的 GPS关掉 然后 打开手机自带的地图 看看能不能定位 如果能 而且啥提示也没有 那就是可以 如果 让你先把GPS打开 那就是不能 反正 我刚试了 iphone上面 不行。我们现在做得 也有这种不想让用户知道程序在使用定位功能的要求,不过我觉得够呛相当地
      

  7.   

    话说你那个基站定位能详细解释下不, 目前用的也只是通过GPS和WIFI的,据说都是通过三点来确定你的地理位置
      

  8.   

    这个我有套代码,但是还不能用,我也不知道为啥 总是报错 跟SIM卡有关,代码如下:package you.packages;public class CellIDInfo {
    public int cellId;
    public String mobileCountryCode;
    public String mobileNetworkCode;
    public int locationAreaCode;
    public String radioType;

    public CellIDInfo(){}
    }package you.packages;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;import android.app.Activity;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.telephony.TelephonyManager;
    import android.telephony.cdma.CdmaCellLocation;
    import android.telephony.gsm.GsmCellLocation;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;public class NetWorkLocationTest extends Activity {    TextView mTextView;
        Button mButton;
        TelephonyManager tm;
        CdmaCellLocation location;
        int cid;  
        int lac;  
      
        int mcc;  
        int mnc;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
            setContentView(R.layout.main);        mTextView = (TextView) findViewById(R.id.TextView01);
            mButton = (Button) findViewById(R.id.Button01);
             tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
            GsmCellLocation gsmCell = (GsmCellLocation) tm.getCellLocation();
            cid = gsmCell.getCid();  
            lac = gsmCell.getLac();  
            String netOperator = tm.getNetworkOperator();  
            mcc = Integer.valueOf(netOperator.substring(0, 3));  
            mnc = Integer.valueOf(netOperator.substring(3, 5));          mButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
            // TODO Auto-generated method stub
    //          tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
             //int type = tm.getNetworkType();
             //if (type ==TelephonyManager.NETWORK_TYPE_CDMA) {
             location = (CdmaCellLocation) tm.getCellLocation();
             if(location == null)
             return;
            int sid = location.getSystemId();//系统标识  mobileNetworkCode
            int bid = location.getBaseStationId();//基站小区号  cellId
            int nid = location.getNetworkId();//网络标识  locationAreaCode
            
            System.out.println("sid:"+ "" + sid);
            System.out.println("bid:"+ "" + bid);
            System.out.println("nid:"+ "" + nid);
            ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
            CellIDInfo info = new CellIDInfo();
            info.cellId = bid;
            info.locationAreaCode = nid;
            info.mobileNetworkCode = String.valueOf(sid);
            info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3);
            info.mobileCountryCode = tm.getNetworkOperator().substring(3, 5);
            info.radioType = "cdma";
            CellID.add(info);
            System.out.println("cellId:"+"" + info.cellId);
            System.out.println("locationAreaCode:"+"" + info.locationAreaCode);
            System.out.println("mobileNetworkCode:"+ info.mobileNetworkCode);
            System.out.println("mobileCountryCode:"+ info.mobileCountryCode);
            Location loc = callGear(CellID);
            
            if (loc !=null) 
             mTextView.setText("纬度:" + loc.getLatitude() + "\n经度:" + loc.getLongitude());
             //}// end if
            }// end onclick        });// end setOnClickListener
        }// end onCreate
     
     
    //调用google gears的方法,该方法调用gears来获取经纬度 
    private Location callGear(ArrayList<CellIDInfo> cellID) {
         if (cellID == null) 
         return null;
        
         DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(
    "http://www.google.com/loc/json");
    JSONObject holder = new JSONObject();
    JSONObject dataT = null; try {
    holder.put("version", "1.1.0");
    holder.put("host", "maps.google.com");
    holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode);
    holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode);
    holder.put("radio_type", cellID.get(0).radioType);
    holder.put("request_address", true);
    if ("460".equals(cellID.get(0).mobileCountryCode)) 
    holder.put("address_language", "zh_CN");
    else
    holder.put("address_language", "en_US");

    dataT = new JSONObject(); 
                dataT.put("cell_id", cid);  
                dataT.put("location_area_code", lac);  
                dataT.put("mobile_countyr_code", mcc);  
                dataT.put("mobile_network_code", mnc); 
                
    JSONObject data,current_data; JSONArray array = new JSONArray();

    current_data = new JSONObject();
    current_data.put("cell_id", cellID.get(0).cellId);
    current_data.put("location_area_code", cellID.get(0).locationAreaCode);
    current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode);
    current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);
    current_data.put("age", 0);
    current_data.put("signal_strength", -60);
    current_data.put("timing_advance", 5555);
    array.put(current_data);
    array.put(dataT);

    holder.put("cell_towers", array);

    StringEntity se = new StringEntity(holder.toString());
    System.out.println("Location send"+holder.toString());
    post.setEntity(se);
    HttpResponse resp = client.execute(post); HttpEntity entity = resp.getEntity(); BufferedReader br = new BufferedReader(
    new InputStreamReader(entity.getContent()));
    StringBuffer sb = new StringBuffer();
    String result = br.readLine();
    while (result != null) {
    Log.e("Locaiton reseive", result);
    sb.append(result);
    result = br.readLine();
    } data = new JSONObject(sb.toString());
    System.out.println("-"+ sb.toString());
    data = (JSONObject) data.get("location"); Location loc = new Location(LocationManager.NETWORK_PROVIDER);
    loc.setLatitude((Double) data.get("latitude"));
    loc.setLongitude((Double) data.get("longitude"));
    loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString()));
    loc.setTime( System.currentTimeMillis());//AppUtil.getUTCTime());
    return loc;
    } catch (JSONException e) {
    e.printStackTrace();
    return null;
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (ClientProtocolException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    System.out.println("-"+ "null 1");
    return null;
    }
    }我还没搞通最近在搞别的
      

  9.   

    对于2.2开始有个api,2.2以前,必须要想方法升到system uid,调用部分方法才能有效的。呵呵,2.2你可以参考下 Android 2.2兼容性移植
      

  10.   

    不知道对不对貌似我记得的是android系统好像不支持CDMA貌似需要转换吧
      

  11.   

    哈哈  我感觉是这样吧  GSM的一套 CDMA的 一套 用什么sim卡就用哪套,哪天我有空也看看 
      

  12.   

    网上搜到的,应该靠谱
     private Location getCurrentLocationGPS() {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            String locationProvider = locationManager.getBestProvider(criteria,
                    true);        Location location = locationManager
                    .getLastKnownLocation(locationProvider);        return location;
        }    // 根据wifi获取当前位置
        private Location getCurrentLocationWifi(Context context) {
            Location location=null;
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            try
            {
                WifiManager wifiManager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
                if(wifiManager.isWifiEnabled())
                {
                 location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                }
            }
            catch(Exception e)
            {
            }
            
            
            
            return location;
        }    // 根据基站获取当前的位置
        private Location getCurrentLocationAGPS() {
            Location location = null;        if (telephonyManager.getCellLocation() == null) {        }
            GsmCellLocation gcl = (GsmCellLocation) telephonyManager
                    .getCellLocation();        int cid = gcl.getCid();        int lac = gcl.getLac();        int mcc = Integer.valueOf(telephonyManager.getNetworkOperator()
                    .substring(0,                3));        int mnc = Integer.valueOf(telephonyManager.getNetworkOperator()
                    .substring(3,                5));        try {            // 组装JSON查询字符串            JSONObject holder = new JSONObject();            holder.put("version", "1.1.0");            holder.put("host", "maps.google.com");            // holder.put("address_language", "zh_CN");            holder.put("request_address", true);            JSONArray array = new JSONArray();            JSONObject data = new JSONObject();
                data.put("cell_id", cid); // 25070
                data.put("location_area_code", lac);// 4474
                data.put("mobile_country_code", mcc);// 460            data.put("mobile_network_code", mnc);// 0
                array.put(data);            holder.put("cell_towers", array);            // 创建连接,发送请求并接受回应            DefaultHttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost("http://www.google.com/loc/json");            StringEntity se = new StringEntity(holder.toString());            post.setEntity(se);            HttpResponse resp = client.execute(post);            HttpEntity entity = resp.getEntity();            BufferedReader br = new BufferedReader(            new InputStreamReader(entity.getContent()));            StringBuffer resultStr = new StringBuffer();            String readLine = null;            while ((readLine = br.readLine()) != null) {                resultStr.append(readLine);            }            JSONObject jsonResult = new JSONObject(resultStr.toString());
                JSONObject jsonLocation = jsonResult.getJSONObject("location");
                double jsonLat = jsonLocation.getDouble("latitude");
                double jsonLon = jsonLocation.getDouble("longitude");            location = new Location("AGPS");
                location.setLatitude(jsonLat);
                location.setLongitude(jsonLon);        } catch (Exception e) {            // TODO: handle exception        }        return location;
        }
      

  13.   

    这个是系统服务层执行的操作,找找GPS C层的代码吧