You need to enable JavaScript to run this app. Flutter开发:Google地图模拟器无法获取当前位置,始终显示GooglePlex位置
优惠活动
大模型
产品
解决方案
定价
更多

Flutter开发:Google地图模拟器无法获取当前位置,始终显示GooglePlex位置

Flutter 获取定位始终返回GooglePlex固定坐标问题排查

问题现象

  • 基于Flutter开发定位功能时,无论是模拟器还是打包APK在物理设备运行,获取到的位置始终固定为GooglePlex坐标(纬度37.42,经度-112.08)
  • 已完成静态权限配置、查阅过公开教程资料,问题仍未解决
  • 现有实现代码如下:
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() => runApp(App());

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {

  String _locationMessage = "";

  void _getCurrentLocation() async {

    final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    print(position);

    setState(() {
      _locationMessage = "${position.latitude}, ${position.longitude}";
    });

  }


  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        home: Scaffold(
            appBar: AppBar(
                title: Text("Location Services")
            ),
            body: Align(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(_locationMessage),
                    FlatButton(
                        onPressed: () {
                          _getCurrentLocation();
                        },
                        color: Colors.green,
                        child: Text("Find Location")
                    )
                  ]),
            )
        )
    );
  }

可落地排查步骤

  • 补全运行时权限校验逻辑
    当前代码仅在AndroidManifest、Info.plist中配置静态权限声明,完全缺失运行时权限检查流程。Android 6.0+、iOS 13+系统要求应用必须在调用定位接口前,主动弹窗引导用户授权定位权限。若权限被拒绝,Geolocator不会抛出异常,而是直接返回内置的GooglePlex默认坐标,没有任何错误提示。
    需要在调用getCurrentPosition前插入完整的权限校验逻辑,参考实现:
    // 检查系统定位开关是否开启
    bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      // 提示用户开启系统定位功能
      return;
    }
    // 检查当前应用的定位权限状态
    LocationPermission permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      // 主动弹出系统权限申请弹窗
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        // 用户拒绝授权,终止流程
        return;
      }
    }
    if (permission == LocationPermission.deniedForever) {
      // 用户永久拒绝授权,引导用户前往系统设置页开启权限
      return;
    }
    // 所有前置校验通过后再获取真实位置
    final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    
  • 修正模拟器模拟定位配置
    Android Studio自带模拟器的默认模拟定位点就是GooglePlex,若未主动修改定位设置,无论应用权限是否正常,都会返回该固定坐标。需要通过模拟器侧边的「Extended controls」面板,在Location选项页手动输入测试用经纬度,点击同步按钮将定位点发送到模拟器,同时注意每次重启模拟器后,定位设置会被重置,需要重新配置。
  • 排查物理设备定位异常原因
    物理设备上返回固定GooglePlex坐标的常见诱因:
    • 系统定位模式被设置为“仅GPS”,在室内无GPS信号的环境下,定位请求超时后会被系统返回兜底默认坐标
    • 应用未被系统授予“始终允许定位”权限,被系统判定为违规获取位置时,直接返回默认坐标
    • 国内网络环境下,Google定位服务无法正常连通,部分定制系统会对未接入系统定位白名单的应用直接返回兜底坐标
  • 校验依赖版本兼容性
    旧版本geolocator包存在Android端权限适配缺陷,在权限流程不完整时会无提示返回GooglePlex默认值,需要检查依赖版本与当前Flutter SDK的匹配关系,升级到官方稳定的正式版本。

内容的提问来源于stack exchange,提问作者BankySam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.14 11:45:03