首页 文章

在Android中获取精确的经度和纬度

提问于
浏览
0

我正在尝试获取手机的纬度和经度,并将这些位置详细信息发送到电子邮件地址 . 它工作正常,但我无法获得当前位置的确切经度和纬度 . 我不想使用 Map 视图,我正在尝试使用GPS,但我无法正常使用它 . 这是我的代码:

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[]{"mallikarjuna8511820@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_CC, new String[]{"mallikarjuna8511820@gmail.com"});
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
}
}

1 回答

  • 0

    你需要在代码中的某个地方requestLocationUpdates,如onCreate / Resume,如:

    lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                        mUpdatePeriod * 1000, mMinResendDistance, this);
    

    典型值可能是:

    private float mMinResendDistance = 10.0f;
    private int mUpdatePeriod; // in SECONDS
    

    .

相关问题