手机的位置

时间:2011-08-15 16:04:06

标签: android android-location

我正在编写一个程序来获取手机使用电话管理器的位置,问题是,它给了我最接近我的蜂窝塔的坐标,但不是我的确切位置。这是我的代码。怎么用细胞塔的coordiantes给我我的exct位置。 Thanx家伙

package com.celllocation;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;
import android.widget.Toast;

public class cellLocation extends Activity {
    int cellID,lac;
    TextView textViewCoord;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textViewCoord=(TextView)findViewById(R.id.textViewCoord);
    }
    @Override
    protected void onStart() {
        TelephonyManager telManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        GsmCellLocation location=(GsmCellLocation)telManager.getCellLocation();
        cellID=location.getCid();
        lac=location.getLac();
        try
        {
            displayMap(cellID, lac);
        }
        catch(Exception e){
            e.printStackTrace();
        }

        super.onStart();
    }
    private boolean displayMap(int cellID, int lac) throws Exception 
    {
        String urlString = "http://www.google.com/glm/mmap";            

        //---open a connection to Google Maps API---
        URL url = new URL(urlString); 
        URLConnection conn = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) conn;        
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true); 
        httpConn.setDoInput(true);
        httpConn.connect(); 

        //---write some custom data to Google Maps API---
        OutputStream outputStream = httpConn.getOutputStream();
        WriteData(outputStream, cellID, lac);       

        //---get the response---
        InputStream inputStream = httpConn.getInputStream();  
        DataInputStream dataInputStream = new DataInputStream(inputStream);

        //---interpret the response obtained---
        dataInputStream.readShort();
        dataInputStream.readByte();
        int code = dataInputStream.readInt();
        if (code == 0) {
            double lat = (double) dataInputStream.readInt() / 1000000D;
            double lng = (double) dataInputStream.readInt() / 1000000D;
            dataInputStream.readInt();
            dataInputStream.readInt();
            dataInputStream.readUTF();

            //---display Google Maps---
            String uriString =lat+ "," + lng;
            Toast.makeText(getApplicationContext(), uriString, Toast.LENGTH_LONG).show();
            textViewCoord.setText(uriString);
            return true;
        }
        else
        {           
            return false;
        }
    } 
    private void WriteData(OutputStream out, int cellID, int lac) throws IOException
    {       
        DataOutputStream dataOutputStream = new DataOutputStream(out);
        dataOutputStream.writeShort(21);
        dataOutputStream.writeLong(0);
        dataOutputStream.writeUTF("en");
        dataOutputStream.writeUTF("Android");
        dataOutputStream.writeUTF("1.0");
        dataOutputStream.writeUTF("Web");
        dataOutputStream.writeByte(27);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(3);
        dataOutputStream.writeUTF("");

        dataOutputStream.writeInt(cellID);  
        dataOutputStream.writeInt(lac);     

        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.flush();       
    }
}

1 个答案:

答案 0 :(得分:1)

对于您需要使用GPS的确切位置,对于不太准确的位置,您可以使用NETWORK_PROVIDER(不同的精度,取决于细胞纽约的密度)。 我认为你可能需要在onCreate中定义一个LocationManager

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

或网络提供商:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

此类提供对系统位置服务的访问。这些服务允许应用程序获取设备地理位置的定期更新,或在设备进入特定地理位置附近时触发应用程序指定的Intent。