纬度和经度值未在TextView中显示

时间:2013-01-11 18:24:04

标签: android xml geolocation

我正在编写一个应用程序,用户可以在活动中填写表单并将其同步到Web服务。部分应用程序涉及地理定位,因此,lat和long值。我希望我的应用程序能够自动地对一个人的智能手机进行地理定位(它成功完成),然后在活动表单上自动填写TextView。我尝试了一些方法,但无济于事。谁能告诉我我哪里出错了?

这是DataEntry类: -

package application.prototype.mfb;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import application.prototype.mfb.MyLocation.LocationResult;

/**
 * Records a new data form entry for a favourite building
 * @author DanielD
 *
 */
public class DataEntry extends Activity implements OnClickListener {

Button btnClear;  
Button btnSync;
EditText txtBuildingName;
EditText txtDescription;
TextView txtLongitude;
TextView txtLatitude;
Spinner project;

public static Location loc;
private static double longitude;
private static double latitude;
private static String name;
private static String description; 
private static String projectString;
private static String lngString = String.valueOf(longitude);
private static String latString = String.valueOf(latitude);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data_entry);

    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            loc = location;
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();
            System.out.println("Latitude: " + latitude);
            System.out.println("Longitude: " + longitude);
        }
    };
        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(DataEntry.this, locationResult);

    txtBuildingName=(EditText)this.findViewById(R.id.nameContent);
    txtDescription=(EditText)this.findViewById(R.id.descriptionContent);

    project=(Spinner)findViewById(R.id.type_project);
    ArrayAdapter<CharSequence> adapterTwo = ArrayAdapter.createFromResource(this, R.array.type_project_array, android.R.layout.simple_spinner_item);
    project.setAdapter(adapterTwo);

    txtLatitude=(TextView)this.findViewById(R.id.TextViewLat);
    txtLongitude=(TextView)this.findViewById(R.id.TextViewLong);

    /*These checks will ensure that if a variable still has user data in it, it will refill the fields with that data preventing users entering
     * the same information multiple times
     */
    if(name != null){
        txtBuildingName.setText(name);
    }

    if(description != null){
        txtDescription.setText(description);
    }

    if(projectString != null){
        project.getItemAtPosition(project.getSelectedItemPosition()).toString();
    }

    if(latString != null){
        txtLatitude.setText(latString);
    }

    if(lngString != null){
        txtLongitude.setText(lngString);
    }

    btnClear=(Button)findViewById(R.id.btnClear);
    btnClear.setOnClickListener(this);

    btnSync=(Button)findViewById(R.id.btnSync);
    btnSync.setOnClickListener(new OnClickListener(){
        public void onClick(View v){                                        //NEW VERSION OF COMMIT DATA - WILL PRESERVE DATA OVER ACTIVITY
            if(txtBuildingName.getText().toString().length() > 0)
                name = txtBuildingName.getText().toString();
            if(txtDescription.getText().toString().length() > 0)
                description = txtDescription.getText().toString();
            if(project.getItemAtPosition(project.getSelectedItemPosition()).toString().length() > 0)
                projectString = project.getItemAtPosition(project.getSelectedItemPosition()).toString();
            Intent intent = new Intent(DataEntry.this, DataSummary.class);
            startActivity(intent);
        }
    });    
}

public static String getName(){
    return name;
}

public static String getDescription(){
    return description;
}

public static String getLongitude(){
    return lngString;
}

public static String getLatitude(){
    return latString;
}

/*public Location getLongitude(){
     LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
     Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
     longitude = location.getLongitude();
     return longitude;
 }

public Location getLatitude(){
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    latitude = location.getLatitude();
    latitudeString = String.valueOf(latitude);
    return latitudeString;

}*/

public static String getProject(){
    return projectString;
}

public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
}

public void onRestoredInstanceState(Bundle returnState)
{
    super.onRestoreInstanceState(returnState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_favourite_building, menu);
    return true;
}

public void onClick(View v){
    switch(v.getId())
    {
    case R.id.btnClear : clearFields();
    break;  
    }
}

/**
 * Switch statement governing the different options available on the Action Bar
 */
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case R.id.app_camera:
        Intent intentZero = new Intent(this, CameraDemo.class);
        intentZero.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intentZero);
        return true;

    case R.id.app_dataentry:  
        Intent intent = new Intent(this, DataEntry.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;

    case R.id.app_syncorsave:
        Intent intentOne = new Intent(this, DataSummary.class);
        intentOne.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intentOne);
        return true;

    case R.id.app_discard:  
        Intent intentTwo = new Intent(this, DeleteProjects.class);
        intentTwo.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intentTwo);
        return true;

    case R.id.app_homescreen:
        Intent intentThree = new Intent(this, ProjectGrid.class);
        intentThree.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intentThree);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

 public void clearFields(){
        Toast.makeText(DataEntry.this, "All fields Reset", Toast.LENGTH_LONG).show();
        txtBuildingName.getEditableText().clear();
        txtDescription.getEditableText().clear();
        name = "";              //must also set the static variables to blank otherwise previous data still assigned in variable.
        description = "";
        projectString = ""; 
    }
}

这是MyLocation.java类: -

package application.prototype.mfb;

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;

public boolean getLocation(Context context, LocationResult result)
{
    //I use LocationResult callback class to pass location value from MyLocation to user code.
    locationResult=result;
    if(lm==null)
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    //exceptions will be thrown if provider is not permitted.
    try
    {
        gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    try
    {
        network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }

    //don't start listeners if no provider is enabled
    if(!gps_enabled && !network_enabled)
        return false;

    if(gps_enabled)
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
    if(network_enabled)
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
    timer1=new Timer();
    timer1.schedule(new GetLastLocation(), 20000);
    return true;
}

LocationListener locationListenerGps = new LocationListener() 
{
    public void onLocationChanged(Location location) 
    {
        timer1.cancel();
        locationResult.gotLocation(location);



      //  lm.removeUpdates(this);
        //lm.removeUpdates(locationListenerNetwork);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

LocationListener locationListenerNetwork = new LocationListener()
{
    public void onLocationChanged(Location location) 
    {
        timer1.cancel();
        locationResult.gotLocation(location);
       // lm.removeUpdates(this);
     //   lm.removeUpdates(locationListenerGps);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

class GetLastLocation extends TimerTask 
{
    @Override
    public void run() {
         //lm.removeUpdates(locationListenerGps);
         //lm.removeUpdates(locationListenerNetwork);

         Location net_loc=null, gps_loc=null;
         if(gps_enabled)
             gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         if(network_enabled)
             net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

         //if there are both values use the latest one
         if(gps_loc!=null && net_loc!=null){
             if(gps_loc.getTime()>net_loc.getTime())
                 locationResult.gotLocation(gps_loc);
             else
                 locationResult.gotLocation(net_loc);
             return;
         }

         if(gps_loc!=null){
             locationResult.gotLocation(gps_loc);
             return;
         }
         if(net_loc!=null){
             locationResult.gotLocation(net_loc);
             return;
         }
         locationResult.gotLocation(null);
    }
}

public static abstract class LocationResult
{
    public abstract void gotLocation(Location location);
}
}

现在我添加了System.out.println方法,以便在logcat日志中识别是否找到了位置值并且它们具有(见下文)。那么如何获取这些值并将它们转换为合适的格式以在TextView中显示?

01-11 18:04:01.885: I/System.out(541): Latitude: -4.066498333333333

01-11 18:04:01.885: I/System.out(541): Longitude: 52.410999999999994

*作为生成的logcat文件中的exert。

4 个答案:

答案 0 :(得分:3)

  

那么我如何获取这些值并将它们转换为合适的格式以在TextView中显示?

显示它们很简单,你完成了98%的工作,只需添加:

LocationResult locationResult = new LocationResult(){
    @Override
    public void gotLocation(Location location){
        loc = location;
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();

        txtLatitude.setText(latitude + "");
        txtLongitude.setText(longitude + "");

        System.out.println("Latitude: " + latitude);
        System.out.println("Longitude: " + longitude);
    }
};

但“合适的格式”有点模糊......你想要什么格式?您可以使用Location.convert()来使用更传统的hour:minute:second.fraction格式。

答案 1 :(得分:1)

TextView tvLongitude=(TextView)findViewById(R.id.textViewlongitude);         
TextView tvLatitude=(TextView)findViewById(R.id.textViewlatitude); 
tvLongitude.setText("Longitude is"+longitude );
tvLatitude.setText("Latitude is"+latitude );

通过这种方式,您可以在textview上设置值

答案 2 :(得分:0)

嗯,快速观察你的代码,你在System.out中打印你在Result回调中收到的double值,但你永远不会填充你试图放在TextViews上的两个String变量。

您应该在位置回调

上重新填充您的变量
  LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            loc = location;
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();
            lngString = String.valueOf(longitude);
            latString = String.valueOf(latitude);
            System.out.println("Latitude: " + latitude);
            System.out.println("Longitude: " + longitude);
        }
    };

答案 3 :(得分:0)

要在textview上显示

您的布局activity_data_entry.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textViewlatitude"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textViewlongitude"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>

内幕代码:

LocationResult locationResult = new LocationResult(){
    @Override
    public void gotLocation(Location location){
        loc = location;
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();

        TextView tvLongitude=(TextView)findViewById(R.id.textViewlongitude);
        TextView tvLatitude=(TextView)findViewById(R.id.textViewlatitude);

        tvLongitude.setText("Longitude is"+longitude );
        tvLatitude.setText("Latitude is"+latitude );
    }
};

但是你的意思是合适的格式?