【发布时间】:2016-04-09 16:01:33
【问题描述】:
我在 Google 上搜索过这个问题,但没有得到 解决方案。我正在尝试获取设备位置的经度和纬度。 用户将单击相机中的图像,那时我必须得到 使用 GPS 的设备的纬度、经度并显示。但不是 工作。
这是我的代码:-
public class MainActivity extends Activity {
EditText editAddress,editName;
ImageView ivPhoto;
Button btnTakePic,btnNext1;
Bitmap bitMap;
static int TAKE_PICTURE = 1;
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected double latitude,longitude;
protected boolean gps_enabled,network_enabled;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editAddress = (EditText)findViewById(R.id.edtAddress);
editName = (EditText)findViewById(R.id.edtName);
ivPhoto = (ImageView)findViewById(R.id.ivPhoto);
btnTakePic = (Button)findViewById(R.id.btnTakePic);
btnNext1 = (Button)findViewById(R.id.btnNext1);
btnTakePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,TAKE_PICTURE);
}
});
btnNext1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nextIntent = new Intent(MainActivity.this,ContactPersonDetail.class);
startActivity(nextIntent);
}
});
}
private void GetLocationLatLong() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
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(MainActivity.this,message,Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){
// get bundle
Bundle extras = intent.getExtras();
// get bitmap
bitMap = (Bitmap) extras.get("data");
ivPhoto.setImageBitmap(bitMap);
GetLocationLatLong();
}
}
private class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
String message = String.format("New Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude() );
longitude = location.getLongitude();
latitude = location.getLatitude();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
Log.d("Longittuteeeeeeeeeeee",message);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(MainActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity.this,"Provider enabled by the user. GPS turned on", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this, "Provider disabled by the user. GPS turned off", Toast.LENGTH_LONG).show();
}
}
}
这些是清单中使用的权限:-
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET"/>
在这些权限中,有两个权限(ACCESS_MOCK_LOCATION,CONTROL_LOCATION_UPDATES)出错。
权限错误:-
使用权限 android:name="android.permission.ACCESS_MOCK_LOCATION"
只能在测试或调试特定的情况下请求模拟位置 清单文件(通常是 src/debug/AndroidManifest.xml)少... (Ctrl+F1) 使用模拟位置提供程序(通过要求权限 android.permission.ACCESS_MOCK_LOCATION)只能在调试中完成 构建(或来自测试)。在 Gradle 项目中,这意味着您应该只 在特定的测试或调试源集中请求此权限 清单文件。要解决此问题,请在调试中创建一个新的清单文件 文件夹并将元素移动到那里。一个典型的路径 Gradle 项目中的调试清单覆盖文件是 src/debug/AndroidManifest.xml.
请让我知道我在这里做错了什么。
【问题讨论】:
-
您使用的代码是为了在位置发生变化时获得更长的时间,但我认为您不需要这样做。您可以通过此链接androidhive.info/2012/07/android-gps-location-manager-tutorial 简单地获取纬度和经度。你只需要getLatitude和getLongitude方法相关的代码
标签: android android-manifest latitude-longitude