如何在没有触摸事件的情况下查找用户当前位置的纬度、经度?我在互联网上搜索但我得到纬度、经度只有触摸事件。
问问题
822 次
2 回答
3
我在这里回答了你的类似问题。看看那个。它不需要谷歌地图。
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"/>
于 2012-08-17T04:27:35.593 回答
1
使用 MyLocationOverlay 获取当前位置。
MyLocationOverlay whereAmI = new MyLocationOverlay(this,mapview);
GeoPoint myLocationGeoPoint = whereAmI.getMyLocation();
float latitude = myLocationGeoPoint.getLatitudeE6() / 1E6;
float longitude = myLocationGeoPoint.getLongitudeE6() / 1E6;
于 2012-08-17T04:31:27.483 回答