How to emulate GPS location in the Android Emulator?

前端 未结 30 2338
天命终不由人
天命终不由人 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条回答
  • 2020-11-21 22:35

    1. Android Studio users.

    After running the emulator goto Tools->Android->Android device monitor

    Click the Emulator Control Tab change from the location controls group.

    2. Eclipse users.

    First In Eclipse In Menu Select "Window" then Select "Open Perspective" then Select "DDMS". i.e Window->Open Prespective->DDMS.

    You will see on Left Side Devices Panel and on Right Side you will see different tabs. Select "Emulator Control" Tab.

    At bottom you will see Location Controls Panel. Select "Manual" Tab.

    Enter Longitude and Latitude in Textboxs then Click Send Button. It will send the position to you emulator and the application.

    3. Using telnet.

    In the run command type this.

    telnet localhost 5554
    

    If you are not using windows you can use any telnet client.

    After connecting with telnet use the following command to send your position to emulator.

    geo fix long lat    
    geo fix -121.45356 46.51119 4392
    

    4. Use the browser based Google maps tool

    There is a program that uses GWT and the Google Maps API to launch a browser-based map tool to set the GPS location in the emulator:

    android-gps-emulator

    0 讨论(0)
  • 2020-11-21 22:36

    Using the "geo" command in the emulator console

    To send mock location data from the command line:

    1. Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory.

    2. Connect to the emulator console:

      telnet localhost 5555 (Replace 5555 with whatever port your emulator is running on)

    3. Send the location data: * geo fix to send a fixed geo-location.

      This command accepts a longitude and latitude in decimal degrees, and an optional altitude in meters. For example:

      geo fix -121.45356 46.51119 4392
      
    0 讨论(0)
  • 2020-11-21 22:36

    There is a plugin for Android Studio called “Mock Location Plugin”. You can emulate multiple points with this plugin. You can find a detailed manual of use in this link: Android Studio. Simulate multiple GPS points with Mock Location Plugin

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-11-21 22:38

    For Android Studio users:

    run the emulator,

    Then, go to Tools -> Android ->Android device monitor

    open the Emulator Control Tab, and use the location controls group.

    0 讨论(0)
  • 2020-11-21 22:40

    If you're using Eclipse, go to Window->Open Perspective->DDMS, then type one in Location Controls and hit Send.

    0 讨论(0)
提交回复
热议问题