获取纬度,经度无触摸

时间:2012-08-17 04:20:34

标签: android google-maps gps

如何在没有触摸事件的情况下找到用户当前位置纬度,经度?我在网上搜索但是我得到纬度,经度只触摸事件。

2 个答案:

答案 0 :(得分:3)

我已经回答了你的类似问题here。看看那个。 它不需要谷歌地图。

public class LocationSampleActivity extends Activity implements OnInitListener 
{
    TextView tv;

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);         
        tv = (TextView)this.findViewById(R.id.txtLocation);
        tts = new TextToSpeech(this, this);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);    
    }

    private class mylocationlistener implements LocationListener 
    {
        @Override 
        public void onLocationChanged(Location location) {    
            if (location != null) {
            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            String str = "\n CurrentLocation: "+
                "\n Latitude: "+ location.getLatitude() + 
                "\n Longitude: " + location.getLongitude();       
              tv.append(str);  
              speak(location);
            } 
        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(LocationSampleActivity.this,"Error onProviderDisabled",Toast.LENGTH_LONG).show();
        }    
        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(LocationSampleActivity.this,"onProviderEnabled",Toast.LENGTH_LONG).show();
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(LocationSampleActivity.this,"onStatusChanged",Toast.LENGTH_LONG).show();
        }
    }
}

它会更新您当前位置的纬度&只是简单地进入TextView而没有触摸。请将重要权限添加到清单文件中。

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />       
<uses-permission android:name="android.permission.INTERNET" />     
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

答案 1 :(得分:1)

使用MyLocationOverlay获取当前位置。

   MyLocationOverlay whereAmI  = new MyLocationOverlay(this,mapview); 

   GeoPoint myLocationGeoPoint = whereAmI.getMyLocation();

   float latitude = myLocationGeoPoint.getLatitudeE6() / 1E6;

   float longitude = myLocationGeoPoint.getLongitudeE6() / 1E6;