我在谷歌搜索过这个问题,但没有得到 我试图获取设备位置的经度和纬度。 用户将点击相机中的图像,那时我必须得到 Lat,Long使用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)给出错误。
许可错误: -
uses-permission android:name =“android.permission.ACCESS_MOCK_LOCATION”
模拟位置只应在测试或特定于调试中请求 清单文件(通常是src / debug / AndroidManifest.xml)少... (Ctrl + F1)使用模拟位置提供程序(通过要求权限) android.permission.ACCESS_MOCK_LOCATION)应该只在调试中完成 构建(或从测试)。在Gradle项目中,这意味着你应该只 在特定的测试或调试源集中请求此权限 清单文件。要解决此问题,请在调试中创建一个新的清单文件 文件夹并将元素移动到那里。一条典型的路径 Gradle项目中的调试清单覆盖文件是 SRC /调试/ AndroidManifest.xml中。
请让我知道我在这里做错了什么。
答案 0 :(得分:0)
GetLocationLatLong函数的这一部分必须在onCreate方法中:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
您应该在该方法之前设置requestLocationUpdates。其余的应该可以正常工作。
此外,您不需要这些权限:
<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" />
答案 1 :(得分:0)
尝试使用此功能。我目前正在使用此设备来获取设备的当前位置。
public class GPSHelper {
private Context mContext;
// flag for GPS Status
private boolean isGPSEnabled = false;
// flag for network status
private boolean isNetworkEnabled = false;
private LocationManager locationManager;
private double latitude;
private double longitude;
public GPSHelper(Context context) {
mContext = context;
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
}
public LatLng getMyLocation() {
List<String> providers = locationManager.getProviders(true);
Location l = null;
for (int i = 0; i < providers.size(); i++) {
l = locationManager.getLastKnownLocation(providers.get(i));
if (l != null)
break;
}
if (l != null) {
latitude = l.getLatitude();
longitude = l.getLongitude();
}
return new LatLng(latitude, longitude);
}
public boolean isGPSenabled() {
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
return (isGPSEnabled || isNetworkEnabled);
}
/**
* Function to get latitude
*/
public double getLatitude() {
return latitude;
}
/**
* Function to get longitude
*/
public double getLongitude() {
return longitude;
}
}
用法:
GPSHelper gpsHelper = new GPSHelper(getActivity());//I'm creating this object inside a fragment
LatLng currentLatLng = gpsHelper.getMyLocation();
currentLat = currentLatLng.latitude;
currentLng = currentLatLng.longitude;
权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />