android:实现纬度 - 经度

时间:2012-01-05 06:09:28

标签: android latitude-longitude

我正在开发一个应用程序,我必须得到latituide和经度详细信息,我得到的详细信息,但当我把这些细节放在http://itouchmap.com/latlong.html我得到错误的位置。我的代码如下:

 Log.v("----------","mlocManager");
    mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 40,  mlocListener);

和位置类如下:

public class MyLocationListener implements LocationListener
 {
    public void onLocationChanged(final Location loc)
    {


        try 
        {

        System.out.println("............ ..............................Location changedin 11");
        latitude = loc.getLatitude();// here the app is getting lat
        longitude = loc.getLongitude(); // and here long
        altitude=loc.getAltitude();

        String s= Double.toString(latitude);
        String lat=s.substring(0, 2);
        String lat1=s.substring(3, 5);
        String lat2=s.substring(5, 7);
        String lat3=s.substring(7, 9);

          finallat=lat+(char) 0x00B0 +lat1+"'"+lat2+"."+lat3+"''";
        System.out.println("finallat"+finallat);


        String s1= Double.toString(longitude);
        String longt=s1.substring(0, 2);
        String longt1=s.substring(3, 5);
        String longt2=s.substring(5, 7);
        String longt3=s.substring(7, 9);

         finalllong=longt+(char) 0x00B0 +longt1+"'"+longt2+"."+longt3+"''";
        System.out.println("finallong:"+finalllong);

        System.out.println("newwwwww"+(char) 0X00B1);

        loc.getAccuracy();
        System.out.println("altitude   : "+loc.hasAltitude());



        latitudee=(TextView)findViewById(R.id.latitudee);
        latitudee.setText(finallat);

        longitudee=(TextView)findViewById(R.id.longitudee);
        longitudee.setText(""+finalllong);

        accuracyy=(TextView)findViewById(R.id.accuracyy);
        accuracyy.setText(""+(char) 0X00B1+loc.getAccuracy()+"m");
        System.out.println("acc : "+""+(char) 0X00B1+loc.getAccuracy()+"m");

        System.out.println("alti:"+altitude);



          }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

1 个答案:

答案 0 :(得分:1)

使用 GPS提供商代替网络提供商,它确实有效,我的代码如下:

mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 40,  mlocListener);