GPS Location abfragen

  • Antworten:12
  • Bentwortet
Susanne Hinteregger
  • Forum-Beiträge: 24

14.11.2012, 10:14:55 via Website

Hallo alle zusammen!

Folgende Aufgabenstellung:
Ich möchte meine aktuelle Location auf einem Handy auslesen können(per GPS) und die Location an ein anderes senden. Das zweite Handy soll dann eine Karte(wie bei Google Maps) angezeigt bekommen. Ich habe gerade versucht mich durch den Dschungel von LocationManager, Location, Criteria usw. zu schlagen. Bin aber zu keiner sinnvollen Lösung gekommen.
Hat jemand von euch einen Vorschlag, wie man dieses Problem am besten lösen könnte?
Bin für jeden Vorschlag dankbar :)

Antworten
Susanne Hinteregger
  • Forum-Beiträge: 24

14.11.2012, 10:45:42 via Website

An allem... Ich weiß nicht genau, wie ich das ganze am besten aufbauen soll.

Ich habe mich gedacht zum Anzeigen der Map, binde ich Google Maps ein. Wie schaut es da mit den Lizenzen aus? Das ist doch open source oder?
Muss ich dann meine GPS Koordinaten überhaupt händisch auslesen?
Gesendet soll das alles über das Internet werden. Ohne Server

Antworten
Phil G.
  • Forum-Beiträge: 86

14.11.2012, 10:56:04 via Website

Dann lies dich doch am besten in die Materie ein.
Zum Thema gibt es jede Menge gute Tutorials.

www.androidhive.info/2012/01/android-working-with-google-maps/

/www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

In diesen oben genannten Tutorials ist auch genau beschrieben wie du einen Key zur GoogleMaps-Nutzung generierst.

Antworten
Phil G.
  • Forum-Beiträge: 86

14.11.2012, 11:33:54 via Website


Ich möchte meine aktuelle Location auf einem Handy auslesen können(per GPS) und die Location an ein anderes senden. Das zweite Handy soll dann eine Karte(wie bei Google Maps) angezeigt bekommen. Ich habe gerade versucht mich durch den Dschungel von LocationManager, Location, Criteria usw. zu schlagen. Bin aber zu keiner sinnvollen Lösung gekommen.

Welche Überlegungen hast du den bereits angestellt ???

-> soll die Position deines Handys permanent an das andere Handy gesandt werden oder nur per "Aktion" (druck auf einen Button)

Mögliches Szenario:

Auf deinem Handy läuft ein Service welcher die aktuelle GPS-Position in einen bestimmten Intervall ausliest (Locationmanager) und bei Veränderung an das andere Handy weitergibt.

-> Wie die Daten weitergeben ?

Deine App sendet die Daten an einen Server und deponiert die Daten dort, das Zielhandy hat ebenfalls einen Service laufen welcher den Server abfragt und die aktuellen Daten auf einer GoogleMap darstellt.
(Form wie die Daten übermittelt werden kannst bzw musst du dir selber überlegen JSON, XML)

Hauptproblem: die Verwendung von Services die permanente Daten senden / Abfragen durchführen verbrauchen halt Akkustrom und mindern die Handy-Laufzeit (-> Polling)

Alternative:

Du nutzt den Google Message Dienst. Damit kannst du das Ganze eleganter lösen.

Antworten
Susanne Hinteregger
  • Forum-Beiträge: 24

21.11.2012, 11:11:33 via Website

Hallo!

Tut mir Leid, dass ich mich erst jetzt wieder melde. Bin einige Zeit ziemlich beschäftigt gewesen.
Ich bin mittlerweile schon etwas weitergekommen. Google Maps habe ich eingebunden.
Hier habe ich im Moment folgendes Problem: Woher bekomme ich die mark_red, wenn ich mit folgenden Code einen Marker setzten möchte

1List<Overlay> mapOverlays = mapView.getOverlays();
2 Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
3 AddItemizedOverlay itemizedOverlay =
4 new AddItemizedOverlay(drawable, this);

Die Position soll nur nach einer bestimmten Aktion gesendet werden(eben z.B. druck auf einen Button etc).

Antworten
Susanne Hinteregger
  • Forum-Beiträge: 24

21.11.2012, 14:52:17 via Website

Achso ich dachte, diese Icons sind vielleicht automatisch dabei und ich habe vergessen irgendwelche google packages einzubinden. Okay ich habe mir jetzt ein eigenes Icon besorgt.
Danke für die geduldigen Antworten :)

Antworten
Klaus
  • Forum-Beiträge: 33

25.11.2012, 02:17:50 via Website

Susanne Hinteregger
Ich habe mich gedacht zum Anzeigen der Map, binde ich Google Maps ein. Wie schaut es da mit den Lizenzen aus? Das ist doch open source oder?

Eine gewisse Anzahl von Kartenladevorgängen ist bei Google Maps frei - je nach genutzter API 2500 oder 25000 -
danach muß pro 1000 Ladevorgängen zwischen $4-$10 gezahlt werden.

Darf leider keinen Link setzen [Um Spam in unserem Forum zu vermeiden, können neue Mitglieder vorerst keine Links auf externe Webseiten veröffentlichen.]

Antworten
Andy N.
  • Forum-Beiträge: 3.112

25.11.2012, 11:18:39 via Website

Google Maps ist nicht Open Source, allerdings für Android Apps weiterhin kostenlos.
Google bietet auch eine gute Dokumentation, ein einfaches Tutorial findet man hier:
https://developers.google.com/maps/documentation/android/hello-mapview

Damit die App läuft musst Du dir einen Schlüssel holen, wie das geht wird aber auch auf der Seite erklärt.

Antworten
Susanne Hinteregger
  • Forum-Beiträge: 24

13.02.2013, 18:45:53 via Website

Ich habe das Ganze jetzt mit folgendem Code gelöst:

ein gps.xml:
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <LinearLayout
8 android:id="@+id/linearLayout1"
9 android:layout_width="match_parent"
10 android:layout_height="wrap_content"
11 android:layout_marginTop="40dip"
12 android:orientation="horizontal" >
13
14 <TextView
15 android:id="@+id/TextView01"
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content"
18 android:layout_marginLeft="10dip"
19 android:layout_marginRight="5dip"
20 android:text="Latitude: "
21 android:textSize="20dip" >
22 </TextView>
23
24 <TextView
25 android:id="@+id/TextView02"
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:text="unknown"
29 android:textSize="20dip" >
30 </TextView>
31 </LinearLayout>
32
33 <LinearLayout
34 android:id="@+id/linearLayout2"
35 android:layout_width="match_parent"
36 android:layout_height="wrap_content" >
37
38 <TextView
39 android:id="@+id/TextView03"
40 android:layout_width="wrap_content"
41 android:layout_height="wrap_content"
42 android:layout_marginLeft="10dip"
43 android:layout_marginRight="5dip"
44 android:text="Longitute: "
45 android:textSize="20dip" >
46 </TextView>
47
48 <TextView
49 android:id="@+id/TextView04"
50 android:layout_width="wrap_content"
51 android:layout_height="wrap_content"
52 android:text="unknown"
53 android:textSize="20dip" >
54 </TextView>
55 </LinearLayout>
56
57</LinearLayout>

und die klasse dazu :

1package com.example.sos;
2
3import android.app.Activity;
4import android.content.Context;
5import android.location.Criteria;
6import android.location.Location;
7import android.location.LocationListener;
8import android.location.LocationManager;
9import android.os.Bundle;
10import android.widget.TextView;
11import android.widget.Toast;
12
13public class ShowLocationActivity extends Activity implements LocationListener {
14 private TextView latituteField;
15 private TextView longitudeField;
16 private LocationManager locationManager;
17 private String provider;
18
19
20/** Called when the activity is first created. */
21
22 @Override
23 public void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.gps);
26 latituteField = (TextView) findViewById(R.id.TextView02);
27 longitudeField = (TextView) findViewById(R.id.TextView04);
28
29 latituteField = (TextView) findViewById(R.id.TextView02);
30 longitudeField = (TextView) findViewById(R.id.TextView04);
31
32 // Get the location manager
33 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
34 // Define the criteria how to select the locatioin provider -> use
35 // default
36 Criteria criteria = new Criteria();
37 provider = locationManager.getBestProvider(criteria, false);
38 Location location = locationManager.getLastKnownLocation(provider);
39
40 // Initialize the location fields
41 if (location != null) {
42 System.out.println("Provider " + provider + " has been selected.");
43 onLocationChanged(location);
44 } else {
45 latituteField.setText("Location not available");
46 longitudeField.setText("Location not available");
47 }
48 }
49
50 /* Request updates at startup */
51 @Override
52 protected void onResume() {
53 super.onResume();
54 locationManager.requestLocationUpdates(provider, 400, 1, this);
55 }
56
57 /* Remove the locationlistener updates when Activity is paused */
58 @Override
59 protected void onPause() {
60 super.onPause();
61 locationManager.removeUpdates(this);
62 }
63
64 public void onLocationChanged(Location location) {
65 double lat = (location.getLatitude());
66 double lng = (location.getLongitude());
67 latituteField.setText(String.valueOf(lat));
68 longitudeField.setText(String.valueOf(lng));
69 }
70
71 public void onStatusChanged(String provider, int status, Bundle extras) {
72 // TODO Auto-generated method stub
73
74 }
75
76 public void onProviderEnabled(String provider) {
77 Toast.makeText(this, "Enabled new provider " + provider,
78 Toast.LENGTH_SHORT).show();
79
80 }
81
82 public void onProviderDisabled(String provider) {
83 Toast.makeText(this, "Disabled provider " + provider,
84 Toast.LENGTH_SHORT).show();
85 }
86}

Jetzt entsteht allerdings ein neues Problem:

Die Variablen latituteField und longitudeField brauche ich eigentlich in der Hauptactivity bzw dann in der Activity, welche die Map anzeigt....
Wie kann ich von einer anderen Acitivity auf diese Variablen zugreifen?
Ich hab da was gehört von OnAcitivityResult und startActivityForResult...
Hab aber keine Ahnung, wie das gehen soll...

Antworten
Susanne Hinteregger
  • Forum-Beiträge: 24

13.03.2013, 09:22:35 via Website

Okay nochmals danke für jede Hilfestellung.
Habe das Ganze jetzt mit folgendem Code gelöst:

1package com.example.sos;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.HashMap;
6import java.util.List;
7
8import android.app.AlertDialog;
9import android.content.Context;
10import android.content.DialogInterface;
11import android.content.Intent;
12import android.location.Location;
13import android.location.LocationListener;
14import android.location.LocationManager;
15import android.os.Bundle;
16import android.provider.Settings;
17import android.util.Log;
18
19//Einfacher Multiplexer für Location-Zeugs
20public class LocationController implements LocationListener {
21 private static LocationController _shared;
22 private ArrayList<LocationListener> _listeners;
23 private LocationManager _locationManager;
24 private AlertDialog currentDialog;
25
26 public static LocationController sharedController() {
27 if(_shared == null)
28 _shared = new LocationController();
29 return _shared;
30 }
31
32 public LocationController() {
33 _listeners = new ArrayList<LocationListener>();
34 }
35
36 public void setContext(final Context ctx) {
37 assert _locationManager == null;
38 _locationManager = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
39 String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
40 HashMap<String, String> askFor = new HashMap<String, String>();
41 askFor.put("network", "Netzwerk");
42 askFor.put("gps", "GPS");
43
44 if(provider != null) {
45 String[] active = provider.split(",");
46 for(String activeProvider : active) {
47 askFor.remove(activeProvider);
48 }
49 }
50 if(askFor.size() != 0) {
51 StringBuilder inactiveProviders = new StringBuilder();
52 Collection<String> values = askFor.values();
53 int idx = 0;
54 for(String inactiveProvider : values){
55 inactiveProviders.append(inactiveProvider);
56 if(idx != values.size() - 1)
57 inactiveProviders.append(" & ");
58 idx++;
59 }
60 Log.d("Location", provider+"");
61
62
63 AlertDialog safe = new AlertDialog.Builder(ctx)
64 .setTitle(inactiveProviders.toString()+" aktivieren!")
65 .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
66 public void onClick(DialogInterface dialog, int whichButton) {
67 LocationController.this.dismissCurrent();
68 Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
69 ctx.startActivity(intent);
70 }
71 }).create();
72
73 safe.show();
74 Log.d("fertig","fertig");
75 }
76 }
77
78 public void dismissCurrent() {
79 if(currentDialog == null) return;
80 currentDialog.dismiss();
81 currentDialog = null;
82 }
83
84 public void startUpdating() {
85 assert _locationManager != null;
86 List<String> providers = _locationManager.getAllProviders();
87 for (int i = 0 ; i < providers.size(); i++){
88 _locationManager.requestLocationUpdates(providers.get(i), 1, 1, this);
89 }
90 }
91
92 public void stopUpdating() {
93 _locationManager.removeUpdates(this);
94 }
95
96 public void addListener(LocationListener listener) {
97 _listeners.add(listener);
98 if(listener instanceof Context)
99 this.setContext((Context)listener);
100 }
101
102 public void removeListener(LocationListener listener) {
103 _listeners.remove(listener);
104 }
105
106 public void onLocationChanged(Location location) {
107 Log.d("Location", "Location changed to ("+location.getLatitude()+"|"+location.getLongitude()+") by Provider ");
108 for(LocationListener listener : _listeners) {
109 listener.onLocationChanged(location);
110 }
111 }
112
113 public void onProviderDisabled(String provider) {
114 for(LocationListener listener : _listeners) {
115 listener.onProviderDisabled(provider);
116 }
117 }
118
119 public void onProviderEnabled(String provider) {
120 for(LocationListener listener : _listeners) {
121 listener.onProviderEnabled(provider);
122 }
123 }
124
125 public void onStatusChanged(String provider, int status, Bundle extras) {
126 for(LocationListener listener : _listeners) {
127 listener.onStatusChanged(provider, status, extras);
128 }
129 }
130
131}

In der Klassen, welche die Position bestimmt (ShowLocationActivity) und jener die die Position dann in der Map anzeigt habe ich den LocationController wie folgt in der onCreate-Funktion eingebudnen:
1LocationController.sharedController().addListener(this);

in onResume gibt es folgenden Aufruf:
1LocationController.sharedController().startUpdating();

in onPause jweils folgendes:
1LocationController.sharedController().stopUpdating();

Somit kann ich von beiden Klassen einfach auf Latitute und Longitude zugreifen :)

Antworten