SoFunction
Updated on 2025-04-10

Solution to simulate GPS functionality always null on Android emulator

We all know that when we develop, we need to simulate GPS on the simulator. We can always null when we are in Location. I checked it online and found the following solution:

The solution of the online hero:
When obtaining GPS information on the simulator, use Location loc = ("gps"); to obtain location information, but loc is often null in debugging, because first you need to manually add GPS information in the simulator. There are two ways to add manually
1. Under eclipse, manually set the latitude and longitude under windows-->open perspective-->DDMS-->Emulator control-->Manual, and press the send button. However, this method has been tested by me and needs to be based on the following method to be effective

2. You can also manually add information under cmd.

(1) First open the emulator, then run cmd, enter telnet localhost 5554 (Note: 5554 is the port of the emulator on the local machine, it may be different. The specific port number is displayed on the upper left of the emulator). This will appear
Android Console: type 'help' for a list of commands OK.

If you are a friend who uses WIN7, the console may prompt that telnet is invalid or something. That is because telnet does not appear by default under WIN7 and needs to be opened manually. Specifically: [1] Control Panel-->Program-->Open or close Windows functions, and then check the Telnet server and Telnet client. [2] Then manually start Telnet in Administrative Tools->Services

(2) Use the geo command to simulate sending GPS signals:
geo fix longitude latitude

(3) At this time, you will find that there is an additional GPS flag on the status bar of the simulator. Then use Location loc = ("gps"); to get the position of the coordinate~~
I actually operated it and found that the error was still reported, and the GPS indicator icon did not appear. I don’t know why. The above solution gave me a prompt. I think the emulator did not enable GPS, and it was not effective when it was enabled in the system settings. Later, I simply ran the Google map directly on the emulator. This time the GPS indicator icon appeared, and then called the method to obtain the location. Also, it is necessary to specify that GPS is used to locate it. If it is another method, the error will still be reported.