如何将地理位置显示为字符串



我想让一个简单的签入作为我的第一个应用程序。我可以得到一个toast消息与正确的长/晚,但我希望能够通过电子邮件发送它。我目前得到我所有的信息从EditText和一个旋转器,但这一个难倒了我。如有任何帮助,不胜感激。

字符串'message'是本质上发送的内容。我希望它在那里是可能的。

主要活动:

package org.example.jamrock.servicecallcheck_in;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
private Spinner spinner;
private Button sendBtn;
EditText editStoreText;
EditText editKxText;
EditText editCommentText;
GetLocation gps;

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

    addListenerOnSpinnerItemSelection();
    addListenerOnButton();
}

//Spinner Selection
private void addListenerOnSpinnerItemSelection() {
    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.service_array, R.layout.spinner_item);
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
private void addListenerOnButton() {
    spinner = (Spinner) findViewById(R.id.spinner);
    sendBtn = (Button) findViewById(R.id.sendBtn);
    sendBtn.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gps = new GetLocation(MainActivity.this);
            if(gps.canGetLocation()) {
                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();
                Toast.makeText(getApplicationContext(), "Your Location is - nLat: " + latitude + "nLong: " + longitude, Toast.LENGTH_LONG).show();

            } else {
                gps.showSettingsAlert();
            }

            editStoreText = (EditText) findViewById(R.id.editstoretext);
            editKxText = (EditText) findViewById(R.id.editkxtext);
            editCommentText = (EditText) findViewById(R.id.editcommenttext);
            String store = editStoreText.getText().toString();
            String kx = editKxText.getText().toString();
            String comment = editCommentText.getText().toString();
            String service = spinner.getSelectedItem().toString();
            String message = "Store Name: " + store + "nnnKX: " + kx  + "nnnStatus: " + service + "nnnComment:" +
                    comment;
            sendBtn(message);


        }
    });

}

GetLocation.java

package org.example.jamrock.servicecallcheck_in;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
/**
* Created by Michael on 5/19/2015.
*/
public class GetLocation extends Service implements LocationListener {
private final Context context;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
public GetLocation(Context context) {
    this.context = context;
    getLocation();
}
public  Location getLocation() {
    try {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if(!isGPSEnabled && !isNetworkEnabled) {

        }   else {
            this.canGetLocation = true;
            if(isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            }
            if(locationManager !=null) {
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if(location != null) {
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                }
            }

        }

        if(isGPSEnabled) {
            if(location == null) {
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if(locationManager == null) {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if(location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
        }


    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

public void stopUsingGPS() {
    if(locationManager != null) {
        locationManager.removeUpdates(GetLocation.this);
    }
}

public double getLatitude () {
    if(location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}
public double getLongitude () {
    if(location != null) {
        longitude = location.getLongitude();

    }
    return  longitude;
}

public boolean canGetLocation(){
    return this.canGetLocation;
}
public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("GPS is settings");
    alertDialog.setMessage("GPS is not enabled.  Do you want to go to the settings menu?");
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

我移动了

 editStoreText = (EditText) findViewById(R.id.editstoretext);
        editKxText = (EditText) findViewById(R.id.editkxtext);
        editCommentText = (EditText) findViewById(R.id.editcommenttext);
        String store = editStoreText.getText().toString();
        String kx = editKxText.getText().toString();
        String comment = editCommentText.getText().toString();
        String service = spinner.getSelectedItem().toString();
        String message = "Store Name: " + store + "nnnKX: " + kx  +       "nnnStatus: " + service + "nnnComment:" +
                comment;
        sendBtn(message);

为:

if(gps.canGetLocation()) {
            double latitude = gps.getLatitude();
            double longitude = gps.getLongitude();
            Toast.makeText(getApplicationContext(), "Your Location is - nLat: " + latitude + "nLong: " + longitude, Toast.LENGTH_LONG).show();

        }

然后在我的消息字符串

中简单地调用纬度和经度
String message = "Store Name: " + store + "nnnKX: " + kx  +       "nnnStatus: " + service + "nnnComment:" +
                comment + "nnnLocation: " latitude + longitude;

最新更新