GPS Koordinaten als String versenden = null null?

  • Antworten:3
Marc Schumacher
  • Forum-Beiträge: 14

17.01.2013, 09:24:02 via Website

Guten Morgen ich habe ein Problem. Und zwar bastel ich momentan an einer art fahrtenbuch app. ich will beim Senden der Darten an meine Seite die aktuelle gps daten mit versenden. Ich habe fürs gps eine extra klasse gemacht und rufe die halt in meiner mainklasse unter meiner ButtonSend Funktion auf. dieses sieht bei mir in etwa so aus :

1GPSmanager gps = new GPSmanager(this);
2
3 if(gps.canGetLocation()){ // gps enabled} // return boolean true/false
4
5 gps.getLatitude(); // returns latitude
6 gps.getLongitude(); // returns longitude
7 Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
8 }
9 else {
10 gps.showSettingsAlert();
11 }
12 skette = "1;"+"5;"+ rfid + ";" + kstand +";" + kstelle + ";" + purpose + ";" + login + ";" + IMEI + ";" + longitude+ ";" + latitude + ";";
13 lbook.execute(rfid,kstand,kstelle,purpose,login,skette,IMEI,longt,latt);

allerdings bekomme ich die Werte long : Null und Lat : Null

DIe Freigabe habe ich in der Manifest eigentlich reingeschrieben also
permission.ACCESS_FINE_LOCATION und permission.INTERNET sind gesetzt.
Ich dachte zuerst das es vielleicht nur am Emulator leigt aber auch auf meinen handy zeigt und sendet er Null, Null

Hat jemand einen TIpp woran das liegen könnte?

Antworten
Christian
  • Forum-Beiträge: 307

17.01.2013, 10:17:33 via Website

Hi,

laut deinem Kommentaren haben gps.getLatitude() und gps.getLongitude() einen Rückgabewert aber irgendwie "speicherst" du den nicht.
Oder soll das so sein?


Mfg Christian

— geändert am 17.01.2013, 10:17:58

Antworten
Marc Schumacher
  • Forum-Beiträge: 14

17.01.2013, 10:22:04 via Website

Hier habe ich einmal meine Komplette gps klasse in Zeile 105 rufe ich doch eigentlich den wert ab und geben ihn zurück oder sehe ich das falsch? :

1public class GPSmanager extends Service implements LocationListener {
2
3 private final Context mContext;
4
5 // flag for GPS status
6 boolean isGPSEnabled = false;
7
8 // flag for network status
9 boolean isNetworkEnabled = false;
10
11 // flag for GPS status
12 boolean canGetLocation = false;
13
14 Location location; // location
15 double latitude; // latitude
16 double longitude; // longitude
17
18 // The minimum distance to change Updates in meters
19 private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
20
21 // The minimum time between updates in milliseconds
22 private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
23
24 // Declaring a Location Manager
25 protected LocationManager locationManager;
26
27 public GPSmanager(Context context) {
28 this.mContext = context;
29 getLocation();
30 }
31
32 public Location getLocation() {
33 try {
34 locationManager = (LocationManager) mContext
35 .getSystemService(LOCATION_SERVICE);
36
37 // getting GPS status
38 isGPSEnabled = locationManager
39 .isProviderEnabled(LocationManager.GPS_PROVIDER);
40
41 // getting network status
42 isNetworkEnabled = locationManager
43 .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
44
45 if (!isGPSEnabled && !isNetworkEnabled) {
46 // no network provider is enabled
47 } else {
48 this.canGetLocation = true;
49 // First get location from Network Provider
50 if (isNetworkEnabled) {
51 locationManager.requestLocationUpdates(
52 LocationManager.NETWORK_PROVIDER,
53 MIN_TIME_BW_UPDATES,
54 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
55 Log.d("Network", "Network");
56 if (locationManager != null) {
57 location = locationManager
58 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
59 if (location != null) {
60 latitude = location.getLatitude();
61 longitude = location.getLongitude();
62 }
63 }
64 }
65 // if GPS Enabled get lat/long using GPS Services
66 if (isGPSEnabled) {
67 if (location == null) {
68 locationManager.requestLocationUpdates(
69 LocationManager.GPS_PROVIDER,
70 MIN_TIME_BW_UPDATES,
71 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
72 Log.d("GPS Enabled", "GPS Enabled");
73 if (locationManager != null) {
74 location = locationManager
75 .getLastKnownLocation(LocationManager.GPS_PROVIDER);
76 if (location != null) {
77 latitude = location.getLatitude();
78 longitude = location.getLongitude();
79 }
80 }
81 }
82 }
83 }
84
85 } catch (Exception e) {
86 e.printStackTrace();
87 }
88
89 return location;
90 }
91
92 /**
93 * Stop using GPS listener
94 * Calling this function will stop using GPS in your app
95 * */
96 public void stopUsingGPS(){
97 if(locationManager != null){
98 locationManager.removeUpdates(GPSmanager.this);
99 }
100 }
101
102 /**
103 * Function to get latitude
104 * */
105 public double getLatitude(){
106 if(location != null){
107 latitude = location.getLatitude();
108 }
109
110 // return latitude
111 return latitude;
112 }
113
114 /**
115 * Function to get longitude
116 * */
117 public double getLongitude(){
118 if(location != null){
119 longitude = location.getLongitude();
120 }
121
122 // return longitude
123 return longitude;
124 }
125
126 /**
127 * Function to check GPS/wifi enabled
128 * @return boolean
129 * */
130 public boolean canGetLocation() {
131 return this.canGetLocation;
132 }
133
134 /**
135 * Function to show settings alert dialog
136 * On pressing Settings button will lauch Settings Options
137 * */
138 public void showSettingsAlert(){
139 AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
140
141 // Setting Dialog Title
142 alertDialog.setTitle("GPS is settings");
143
144 // Setting Dialog Message
145 alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
146
147 // On pressing Settings button
148 alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
149 public void onClick(DialogInterface dialog,int which) {
150 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
151 mContext.startActivity(intent);
152 }
153 });
154
155 // on pressing cancel button
156 alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
157 public void onClick(DialogInterface dialog, int which) {
158 dialog.cancel();
159 }
160 });
161
162 // Showing Alert Message
163 alertDialog.show();
164 }
165 public void onLocationChanged(Location location) {
166 }
167
168 public void onProviderDisabled(String provider) {
169 }
170
171 public void onProviderEnabled(String provider) {
172 }
173
174 public void onStatusChanged(String provider, int status, Bundle extras) {
175 }
176
177 @Override
178 public IBinder onBind(Intent arg0) {
179 return null;
180 }
181
182}

Antworten
Gelöschter Account
  • Forum-Beiträge: 694

17.01.2013, 10:50:58 via Website

Zwei Anmerkungen:

* Vielleicht gibt es keine Known Location ...

Da Du den onLocationChanged nicht nutzt hat Deine Klasse keine Möglichkeit eine aktuelle GPS Location zu erhalten. Somit ist diese Klasse witzlos - und eigentlich ist dieser Service vollständig nutzlos da sie nichts anderes macht als die letzte bekannte Position zu ermitteln. Dafür benötigst Du keinen Service, das kannst Du InLine machen.

*
this.mContext = context;

Der ist lustig. Das Android Team hat den "m" eingeführt da sie eine potentielle Fehlerquelle eleminieren wollen (context=context). Der typische Entwickler soll ja so blöd sein und die Warnungen seines Eclipse übersehen ...

Also entweder:

1mContext = context;

oder

1this.context = context;

Ich persönlich favorisiere die "alte" Fassung ...

Antworten