title: Get precise Android GPS-Location in Python
body: <p>I try to obtain the GPS-Location of my Android Phone in Python (using QPython3 app). This kind of works, but it seems there are several LocationProviders in Android:</p>
  
  <ul>
  <li><strong>gps:</strong> pure gps location, slow, energy consuming, but very accurate, 
  and exactly what I need.</li>
  <li><strong>network:</strong> mix of gps and wifi/cell locating, faster, but less accurate</li>
  <li><strong>passive:</strong> like above but completely without using gps</li>
  </ul>
  
  <p><strong>Problem:</strong>
  When I run my script (below) I only get my location provided by "network"
  wich is not accurate enough.</p>
  
  <p>But I can't find a way to force a specific LocationProvider.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code># import needed modules
  import android
  import time
  import sys, select, os #for loop exit
  
  #Initiate android-module
  droid = android.Android()
  
  #notify me
  droid.makeToast("fetching GPS data")
  
  print("start gps-sensor...")
  droid.startLocating()
  
  while True:
      #exit loop hook
      if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
          line = input()
          print("exit endless loop...")
          break
  
      #wait for location-event
      event = droid.eventWaitFor('location',10000).result
      if event['name'] == "location":
          try:
              #try to get gps location data
              timestamp = repr(event['data']['gps']['time'])
              longitude = repr(event['data']['gps']['longitude'])
              latitude = repr(event['data']['gps']['latitude'])
              altitude = repr(event['data']['gps']['altitude'])
              speed = repr(event['data']['gps']['speed'])
              accuracy = repr(event['data']['gps']['accuracy'])
              loctype = "gps"
          except KeyError:
              #if no gps data, get the network location instead (inaccurate)
              timestamp = repr(event['data']['network']['time'])
              longitude = repr(event['data']['network']['longitude'])
              latitude = repr(event['data']['network']['latitude'])
              altitude = repr(event['data']['network']['altitude'])
              speed = repr(event['data']['network']['speed'])
              accuracy = repr(event['data']['network']['accuracy'])
              loctype = "net"
  
          data = loctype + ";" + timestamp + ";" + longitude + ";" + latitude + ";" + altitude + ";" + speed + ";" + accuracy
  
      print(data) #logging
      time.sleep(5) #wait for 5 seconds
  
  print("stop gps-sensor...")
  droid.stopLocating()
  </code></pre>
  
  <p><strong>Sample Output (fake coordinates):</strong></p>
  
  <pre><code>net;1429704519675;37.235065;-115.811117;0;0;23
  net;1429704519675;37.235065;-115.811117;0;0;23
  net;1429704519675;37.235065;-115.811117;0;0;23
  </code></pre>
  
  <p><strong>Summarization:</strong>
  How do I get a precise GPS location in Android using Python?</p>
  
  <p>Thanks in advance everyone!</p>
  
  <p>EDIT: already tested:</p>
  
  <ul>
  <li>inside / outside </li>
  <li>enabled / disabled </li>
  <li>WiFi GPS enabled (before running script)</li>
  </ul>