0
我想獲取手機的經緯度,並將這些位置詳細信息發送到一個電子郵件地址。它正在工作,但我無法獲得當前位置的確切經度和緯度。我不想使用地圖視圖,我試圖使用GPS,但我無法正確使用它。這是我的代碼:獲取Android中的確切緯度和經度
package com.example.mapq;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MapActivity extends Activity implements LocationListener{
LocationManager lm;
TextView lt, ln;
Button b1;
String provider;
Location l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
ln=(TextView)findViewById(R.id.lng);
lt=(TextView)findViewById(R.id.lat);
b1=(Button) findViewById(R.id.button1);
lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Criteria c=new Criteria();
//criteria object will select best service based on
//Accuracy, power consumption, response, bearing and monetary cost
//set false to use best service otherwise it will select the default Sim network
//and give the location based on sim network
//now it will first check satellite than Internet than Sim network location
provider=lm.getBestProvider(c, false);
//now you have best provider
//get location
l=lm.getLastKnownLocation(provider);
if(l!=null)
{
//get latitude and longitude of the location
final double lng=l.getLongitude();
final double lat=l.getLatitude();
//display on text view
ln.setText(""+lng);
lt.setText(""+lat);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
emailIntent.putExtra(Intent.EXTRA_CC, new String[]{"[email protected]"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "body");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"http://maps.google.com/maps?q=loc:" + lat +","+ lng);
emailIntent.setType("plain/text");
startActivity(Intent.createChooser(emailIntent, "choose email client..."));
}
});
}
else
{
ln.setText("No Provider");
lt.setText("No Provider");
}
}
//If you want location on changing place also than use below method
//otherwise remove all below methods and don't implement location listener
@Override
public void onLocationChanged(Location arg0)
{
double lng=l.getLongitude();
double lat=l.getLatitude();
ln.setText(""+lng);
lt.setText(""+lat);
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
不是指Java而不是JavaScript? –
你期待什麼結果,你會得到什麼? –