You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Java环境下如何移除Firebase存储坐标的lat/lng:前缀

解决方法

方法1:直接拼接坐标字符串(推荐)

不要依赖LatLng.toString()的输出格式,直接通过getLatitude()和getLongitude()获取坐标值,自己拼接成目标格式,这种方式更稳定,不会因为Google更新LatLng类的toString()实现而失效。

修改后的代码片段:

locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(@NonNull LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {
                LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.addMarker(new MarkerOptions().position(userLocation).title("Current Location"));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
                // 直接拼接坐标字符串
                double latitude = userLocation.getLatitude();
                double longitude = userLocation.getLongitude();
                latlang = String.format("(%.7f,%.7f)", latitude, longitude);
            }
        }
    };
    fetchUserLocation();

%.7f用于保留7位小数,和原toString()输出的精度一致,你可以根据需求调整小数位数。

方法2:处理toString()的结果

如果一定要使用LatLng.toString(),可以通过字符串操作移除前缀:

方式A:替换前缀为空字符串

latlang = userLocation.toString().replace("lat/lng: ", "");

方式B:截取子串

通过前缀的长度直接截取后面的内容:

latlang = userLocation.toString().substring("lat/lng: ".length());

方式C:根据"("的位置截取

找到坐标起始的括号位置,截取从该位置到结尾的部分:

String locationStr = userLocation.toString();
int startIndex = locationStr.indexOf("(");
if (startIndex != -1) {
    latlang = locationStr.substring(startIndex);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 11:10:18