我正在开发一个用户将报告字段发生率的应用。 事件位置(经度和纬度)对于映射目的很重要。
为了更好,我只想使用GPS提供商。
在我的活动中,我有一个用于获取位置信息的嵌套类。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="invoice_list">
<tbody class="clickable">
<tr>
<td class="name?ajax_invoice_id">85</td>
<td>31-10-2015 16:54:27</td>
<td>31-10-2015 00:00:00</td>
<td>31-10-2015 00:00:00</td>
<td>4800,00</td>
<td>6000,00</td>
<td>1200,00</td>
<td>25,00 %</td>
<td>1</td>
<td>6,00</td>
<td>800,00</td>
<td class="name?ajax_sent">
<input data-val="true" data-val-required="The Sendt field is required." id="Collection_0__Sent" name="Collection[0].Sent" type="checkbox" value="true" />
<input name="Collection[0].Sent" type="hidden" value="false" />
</td>
<td class="name?ajax_payed">
<input data-val="true" data-val-required="The Betalt field is required." id="Collection_0__Payed" name="Collection[0].Payed" type="checkbox" value="true" />
<input name="Collection[0].Payed" type="hidden" value="false" />
</td>
</tr>
<tr>
<td class="name?ajax_invoice_id">36</td>
<td>25-10-2015 18:54:35</td>
<td>25-10-2015 00:00:00</td>
<td>25-10-2015 00:00:00</td>
<td>6400,00</td>
<td>8000,00</td>
<td>1600,00</td>
<td>25,00 %</td>
<td>1</td>
<td>8,00</td>
<td>800,00</td>
<td class="name?ajax_sent">
<input data-val="true" data-val-required="The Sendt field is required." id="Collection_1__Sent" name="Collection[1].Sent" type="checkbox" value="true" />
<input name="Collection[1].Sent" type="hidden" value="false" />
</td>
<td class="name?ajax_payed">
<input data-val="true" data-val-required="The Betalt field is required." id="Collection_1__Payed" name="Collection[1].Payed" type="checkbox" value="true" />
<input name="Collection[1].Payed" type="hidden" value="false" />
</td>
</tr>
</tbody>
</table>
在我的活动中,我尝试通过
获取位置信息private class myGPSTracker extends Service implements LocationListener {
public myGPSTracker() {
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
//isNetworkEnabled = locationManager
// .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled ) {
// no network provider is enabled
showSettingsAlert();
canGetLocation = true;
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if ( ActivityCompat.checkSelfPermission(ReportIncident.this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(ReportIncident.this, new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, ACCESS_FINE_LOCATION );
}
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
} else {
canGetLocation = true;
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if ( ContextCompat.checkSelfPermission(ReportIncident.this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(ReportIncident.this, new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, ACCESS_FINE_LOCATION );
}
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public double getAltitude()
{
if(location != null){
altitude = location.getAltitude();
}
// return longitude
return altitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public String getAddress()
{
Geocoder geocoder =
new Geocoder(context, Locale.getDefault());
// Create a list to contain the result address
List<Address> addresses = null;
try {
/*
* Return 1 address.
*/
addresses = geocoder.getFromLocation(this.getLatitude(),
this.getLongitude(), 1);
} catch (Exception e1) {
e1.printStackTrace();
return ("Un Known place!");
}
// If the reverse geocode returned an address
if (addresses != null && addresses.size() > 0) {
// Get the first address
Address address = addresses.get(0);
/*
* Format the first line of address (if available),
* city, and country name.
*/
String addressText = String.format(
"%s, %s, %s",
// If there's a street address, add it
address.getMaxAddressLineIndex() > 0 ?
address.getAddressLine(0) : "",
// Locality is usually a city
address.getLocality(),
// The country of the address
address.getCountryName());
// Return the text
return addressText;
} else {
return "Un known place";
}
}
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ReportIncident.this);
// Setting Dialog Title
alertDialog.setTitle("GPS settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
当我运行应用程序时,经度和纬度得到0,我也调试并发现位置管理器为空。
以下是活动的完整代码。
gpstracker=new myGPSTracker();
Incidents incidents =new Incidents();
incidents.setId(new Date().getTime());
incidents.setIncidentdate(new Date().getTime());
incidents.setPlaceName("Some Thing");
incidents.setAltitude(gpstracker.getAltitude());
incidents.setMyLat(gpstracker.getLatitude());
incidents.setMyLongtitude(gpstracker.getLongitude());
缺少什么?
任何帮助都将受到高度赞赏。
罗纳德