为啥我得到 Lat Long 0.0?
Posted
技术标签:
【中文标题】为啥我得到 Lat Long 0.0?【英文标题】:Why I am getting Lat Long 0.0?为什么我得到 Lat Long 0.0? 【发布时间】:2020-01-20 02:42:39 【问题描述】:点击后默认数据保存 静态数据保存
我正在正确获取当前位置,但是当我将 lat 和 lang 存储到我的 sqlite 数据库中时,我将 lat 和 long 0.0 存储到我的表中。它显示 lat 和 lang 0.0,其他值为 null。
public class MapActivity extends FragmentActivity implements OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,
LocationListener
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
TextView add;
Button button;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
String getbuildingname,locality,subLocality,current_state,current_country,postal_code;
double longitude,latitude;
private GoogleMap mMap;
DatabaseHelper databaseHelper;
String lat = String.valueOf(latitude);
String lng = String.valueOf(longitude);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
databaseHelper = new DatabaseHelper(this);
button = findViewById(R.id.button);
add = (TextView)findViewById(R.id.current_location);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
checkLocationPermission();
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
add_location();
// Map ready method
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED)
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
else
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
protected synchronized void buildGoogleApiClient()
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(MapActivity.this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
@Override
public void onConnected(Bundle bundle)
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED)
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
@Override
public void onConnectionSuspended(int i)
@Override
public void onLocationChanged(Location location)
mLastLocation = location;
if (mCurrLocationMarker != null)
mCurrLocationMarker.remove();
//Showing Current Location Marker on Map
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
return;
assert provider != null;
Location locations = locationManager.getLastKnownLocation(provider);
List<String> providerList = locationManager.getAllProviders();
if (null != locations && providerList.size() > 0)
longitude = locations.getLongitude();
latitude = locations.getLatitude();
// Geo Coder
Geocoder geocoder = new Geocoder(getApplicationContext(),
Locale.getDefault());
try
List<Address> listAddresses = geocoder.getFromLocation(latitude,
longitude, 1);
if (null != listAddresses && listAddresses.size() > 0)
getbuildingname = listAddresses.get(0).getPremises();
locality = listAddresses.get(0).getLocality();
subLocality = listAddresses.get(0).getSubLocality();//
current_state = listAddresses.get(0).getAdminArea(); //
current_country = listAddresses.get(0).getCountryName(); // India
postal_code = listAddresses.get(0).getPostalCode(); //
markerOptions.title(""+getbuildingname+","+locality+"," + subLocality + "," +
current_state + "," + current_country + "," + postal_code);
catch (IOException e)
e.printStackTrace();
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
mCurrLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
if (mGoogleApiClient != null)
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,
this);
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
public boolean checkLocationPermission()
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION))
ActivityCompat.requestPermissions(this,
new String[]Manifest.permission.ACCESS_FINE_LOCATION,
MY_PERMISSIONS_REQUEST_LOCATION);
else
ActivityCompat.requestPermissions(this,
new String[]Manifest.permission.ACCESS_FINE_LOCATION,
MY_PERMISSIONS_REQUEST_LOCATION);
return false;
else
return true;
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults)
switch (requestCode)
case MY_PERMISSIONS_REQUEST_LOCATION:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED)
if (mGoogleApiClient == null)
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
else
Toast.makeText(this, "permission denied",
Toast.LENGTH_LONG).show();
return;
private void add_location()
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
databaseHelper.insert_location(lat,lng,locality,current_state,current_country);
Toast.makeText(MapActivity.this, locality, Toast.LENGTH_SHORT).show();
);
//Permissions
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
// Database Helper
boolean insert_location(String lat , String lang , String state , String country, String pin)
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Location_Table_Column2,lat);
values.put(Location_Table_Column3,lang);
values.put(Location_Table_Column5,state);
values.put(Location_Table_Column6,country);
values.put(Location_Table_Column7,pin);
long result =database.insert(Table_Name_Loaction,null,values);
return result != -1;
很多时候,当我点击 pin 时,地址标题会消失。 表创建成功,lat和lang保存为0.0,其他值为null。
【问题讨论】:
您使用的是什么数据类型?使用 REAL 数据类型,或者更喜欢使用 TEXT 并进行转换 @MohanKumar 我仍然遇到同样的问题。 【参考方案1】:愚蠢的错误
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
String lat = String.valueOf(latitude);
String lng = String.valueOf(longitude);
databaseHelper.insert_location(lat,lng,locality,current_state,current_country);
Toast.makeText(MapActivity.this, locality, Toast.LENGTH_SHORT).show();
【讨论】:
【参考方案2】:它会在检测到它不为空时捕获该位置和其他位置
Geocoder geocoder;
List<Address> listAddresses;
geocoder = new Geocoder(this, Locale.getDefault());
try
listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if(!listAddresses.get(0).getLocality(0).equals("") || !listAddresses.get(0).getAdminArea(0).equals("") || !listAddresses.get(0).getCountryName(0).equals(""))
locality = listAddresses.get(0).getLocality(0);
current_state = listAddresses.get(0).getAdminArea(0);
current_country = listAddresses.get(0).getCountryName(0);
catch (IOException e)
e.printStackTrace();
并将其添加到您的 onClick 上,它将验证您的输入
if(lat.equal("") ||lng.equal("") ||locality.equal("") ||current_state.equal("") ||current_country.equal("") )
Toast.makeText(MapsActivity.this, "Somethings Empty", Toast.LENGTH_SHORT).show();
else
databaseHelper.insert_location(lat,lng,locality,current_state,current_country);
Toast.makeText(MapActivity.this, locality, Toast.LENGTH_SHORT).show();
【讨论】:
没有静态值保存我检查了。 databaseHelper.insert_location(latitude,longitude,locality,current_state,current_country);在这一行你确定所有数据都不为空吗? 是的..我现在改变了,我通过转换为 String 来存储经纬度数据。 那么问题一定出在你的DatabaseHelper.java 我发布了数据库辅助函数以上是关于为啥我得到 Lat Long 0.0?的主要内容,如果未能解决你的问题,请参考以下文章
为啥这个用于 lat/long 的 Lua Haversine 代码不起作用?