How to emulate GPS location in the Android Emulator?

前端 未结 30 2727
天命终不由人
天命终不由人 2020-11-21 22:15

I want to get longitude and latitude in Android emulator for testing.

Can any one guide me how to achieve this?

How do I set the location of the emulator to

30条回答
  •  萌比男神i
    2020-11-21 22:37

    I wrote a python script to push gps locations to the emulator via telnet. It defines a source and a destination location. There is also a time offset which lets you control how long coordinates will be pushed to the device. One location is beeing pushed once a second.

    In the example below the script moves from Berlin to Hamburg in 120 seconds. One step/gps location per second with random distances.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    import sys
    import telnetlib
    from time import sleep
    import random
    
    HOST = "127.0.0.1"
    PORT = 5554
    TIMEOUT = 10
    LAT_SRC = 52.5243700
    LNG_SRC = 13.4105300
    LAT_DST = 53.5753200
    LNG_DST = 10.0153400
    SECONDS = 120
    
    LAT_MAX_STEP = ((max(LAT_DST, LAT_SRC) - min(LAT_DST, LAT_SRC)) / SECONDS) * 2
    LNG_MAX_STEP = ((max(LNG_DST, LNG_SRC) - min(LNG_DST, LNG_SRC)) / SECONDS) * 2
    
    DIRECTION_LAT = 1 if LAT_DST - LAT_SRC > 0 else -1
    DIRECTION_LNG = 1 if LNG_DST - LNG_SRC > 0 else -1
    
    lat = LAT_SRC
    lng = LNG_SRC
    
    tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)
    tn.set_debuglevel(9)
    tn.read_until("OK", 5)
    
    tn.write("geo fix {0} {1}\n".format(LNG_SRC, LAT_SRC))
    #tn.write("exit\n")
    
    for i in range(SECONDS):
        lat += round(random.uniform(0, LAT_MAX_STEP), 7) * DIRECTION_LAT
        lng += round(random.uniform(0, LNG_MAX_STEP), 7) * DIRECTION_LNG
    
        #tn.read_until("OK", 5)
        tn.write("geo fix {0} {1}\n".format(lng, lat))
        #tn.write("exit\n")
        sleep(1)
    
    tn.write("geo fix {0} {1}\n".format(LNG_DST, LAT_DST))
    tn.write("exit\n")
    
    print tn.read_all()
    

提交回复
热议问题