如何在xml屏幕上显示'double'纬度和经度字符串
How to display 'double' latitude and longitude to string on the xml screen
我需要有关在屏幕上显示经度和纬度的帮助(geolocation.xml)。
当按下 'retrive location' 按钮时,Toast 消息给出位置坐标右(经度和纬度)的值。
但是当我试图在 geolocation.xml 上显示它时 @id/tvLatitude 和 @id/tvLongitude 没有显示任何东西(只是空白)。
经度和纬度在 'double'.
这是代码的一部分:
> 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 编程的新手。
非常感谢。
这是我的 Activity(已满)。 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();
> }
>
> }
> }
改为
> 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;
> private TextView tv1,tv2;
>
> @Override
> public void onCreate(Bundle savedInstanceState) {
>
> super.onCreate(savedInstanceState);
> setContentView(R.layout.geolocation);
>
> retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
tv1 = (TextView)findViewById(R.id.tvLatitude);
tv2 = (TextView)findViewById(R.id.tvLongitude);
> 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();
>
>
> tv1.setText(""+location.getLatitude());
> tv2.setText(""+location.getLongitude());
>
> }
>
> }
我需要有关在屏幕上显示经度和纬度的帮助(geolocation.xml)。 当按下 'retrive location' 按钮时,Toast 消息给出位置坐标右(经度和纬度)的值。
但是当我试图在 geolocation.xml 上显示它时 @id/tvLatitude 和 @id/tvLongitude 没有显示任何东西(只是空白)。
经度和纬度在 'double'.
这是代码的一部分:
> 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 编程的新手。 非常感谢。
这是我的 Activity(已满)。 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();
> }
>
> }
> }
改为
> 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;
> private TextView tv1,tv2;
>
> @Override
> public void onCreate(Bundle savedInstanceState) {
>
> super.onCreate(savedInstanceState);
> setContentView(R.layout.geolocation);
>
> retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
tv1 = (TextView)findViewById(R.id.tvLatitude);
tv2 = (TextView)findViewById(R.id.tvLongitude);
> 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();
>
>
> tv1.setText(""+location.getLatitude());
> tv2.setText(""+location.getLongitude());
>
> }
>
> }