我需要帮助在屏幕上显示经度和纬度(在geolocation.xml上)。 当按下“反向位置”按钮时,Toast消息会给出位置坐标右侧(经度和纬度)的值。
但是当我试图在geolocation.xml上显示它时,@ id / tvLatitude和@ id / tvLongitude没有显示任何内容(只是空白)。
经度和纬度是'双倍'。
这是代码的一部分:
> protected void showCurrentLocation() {
>
> Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
>
> if (location != null) {
> String message = String.format(
> "Current Location \n Longitude: %1$s \n Latitude: %2$s",
> location.getLongitude(), location.getLatitude()
> );
> Toast.makeText(LbsGeocodingActivity.this, message,
> Toast.LENGTH_LONG).show();
>
> String tvLongitude = String.valueOf(location.getLongitude());
> String tvLatitude = String.valueOf(location.getLatitude());
>
>
>
> }
我的屏幕geolocation.xml是:
> android:layout_width="wrap_content"
>
> android:layout_height="wrap_content"
>
> />
>
> <TextView android:layout_width="fill_parent"
> android:id="@+id/texLongitude "
> android:layout_height="wrap_content"
> android:text="Outlet Longitude"
> android:paddingLeft="10dip"
> android:paddingRight="10dip"
> android:paddingTop="10dip"
> android:textSize="17dip"/>
>
> <!-- Input Name -->
> <TextView
> android:id="@+id/tvLongitude"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:layout_margin="5dip"
> android:layout_marginBottom="15dip"
>
> android:singleLine="true"/>
>
>
> <TextView android:layout_width="fill_parent"
> android:id="@+id/texLatitude "
> android:layout_height="wrap_content"
> android:text="Outlet Latitude"
> android:paddingLeft="10dip"
> android:paddingRight="10dip"
> android:paddingTop="10dip"
> android:textSize="17dip"/>
>
> <!-- Input Name -->
> <TextView
> android:id="@+id/tvLatitude"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:layout_margin="5dip"
> android:layout_marginBottom="15dip"
>
> android:singleLine="true"/>
>
>
> </LinearLayout>
使用String.valueOf(location.getLongitude())是否是在屏幕上显示double to string的正确方法?还有其他办法吗?
我是android和java编程的新手。 非常感谢。
这是我的活动(完整版)。 showCurrentLocation是一个方法。
> public class LbsGeocodingActivity extends Activity {
>
> private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 10; // in Meters
> private static final long MINIMUM_TIME_BETWEEN_UPDATES = 60000; // in Milliseconds
>
>
> protected LocationManager locationManager;
>
> protected Button retrieveLocationButton;
>
> @Override
> public void onCreate(Bundle savedInstanceState) {
>
> super.onCreate(savedInstanceState);
> setContentView(R.layout.geolocation);
>
> retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
>
> locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
>
> locationManager.requestLocationUpdates(
> LocationManager.GPS_PROVIDER,
> MINIMUM_TIME_BETWEEN_UPDATES,
> MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
> new MyLocationListener()
> );
>
> retrieveLocationButton.setOnClickListener(new OnClickListener() {
> @Override
> public void onClick(View v) {
> showCurrentLocation();
> }
> });
>
> }
>
> protected void showCurrentLocation() {
>
> Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
>
> if (location != null) {
> String message = String.format(
> "Current Location \n Longitude: %1$s \n Latitude: %2$s",
> location.getLongitude(), location.getLatitude()
> );
> Toast.makeText(LbsGeocodingActivity.this, message,
> Toast.LENGTH_LONG).show();
>
>
> ((TextView)findViewById(R.id.tvLatitude)).setText(""+location.getLatitude());
> ((TextView)findViewById(R.id.tvLongitude)).setText(""+location.getLongitude());
>
> }
>
> }
>
> private class MyLocationListener implements LocationListener {
>
> public void onLocationChanged(Location location) {
> String message = String.format(
> "New Location \n Longitude: %1$s \n Latitude: %2$s",
> location.getLongitude(), location.getLatitude()
> );
> Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
> }
>
> public void onStatusChanged(String s, int i, Bundle b) {
> Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
> Toast.LENGTH_LONG).show();
> }
>
> public void onProviderDisabled(String s) {
> Toast.makeText(LbsGeocodingActivity.this,
> "Provider disabled by the user. GPS turned off",
> Toast.LENGTH_LONG).show();
> }
>
> public void onProviderEnabled(String s) {
> Toast.makeText(LbsGeocodingActivity.this,
> "Provider enabled by the user. GPS turned on",
> Toast.LENGTH_LONG).show();
> }
>
> }
> }