Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将地质点转换为json字符串问题_Android_Json_Gps_Location_Gson - Fatal编程技术网

Android 将地质点转换为json字符串问题

Android 将地质点转换为json字符串问题,android,json,gps,location,gson,Android,Json,Gps,Location,Gson,我正在使用Gson将包含两个GeoPoints的类转换为JSON字符串。下面的代码在我的类中实现LocationListener lat = (float) loc.getLatitude(); lng = (float) loc.getLongitude(); alt = (float) loc.getAltitude(); GeoPoint nextGeoPoint = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6)); MapPlot ma

我正在使用Gson将包含两个
GeoPoints
的类转换为JSON字符串。下面的代码在我的类中实现
LocationListener

lat = (float) loc.getLatitude();
lng = (float) loc.getLongitude();
alt = (float) loc.getAltitude();

GeoPoint nextGeoPoint = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
MapPlot mapleg = new MapPlot();
mapleg.fromPoint = LastGeoPoint;
mapleg.toPoint = nextGeoPoint;              

Gson gson = new Gson();
String jsonString = gson.toJson(mapleg); //convert the mapleg class to a json string
我希望JSON字符串包含两组纬度和经度,但生成了以下JSON字符串

{"fromPoint":{"mMapPoint":{"latitudeE6":33736724,"longitudeE6":-118101837,"pixelCoordX":92309232,"pixelCoordY":214935878}},"toPoint":{"mMapPoint":{"latitudeE6":37422004,"longitudeE6":-122084091,"pixelCoordX":86370464,"pixelCoordY":208176089}}}

像素坐标是什么?
GeoPoint
上的文档没有显示任何与像素有关的内容。当我稍后获取这些数据并将其绘制在
mapView
上时,我解决了这个问题,但我还没有到达那里。奇怪。

GSON查看的是对象上的字段,而不是getter方法,而地质点实际上拥有这些私有字段以及您感兴趣的字段。请参见此处,以获取使这些字段可在地质点上访问的请求:

因此,GSON基本上尝试序列化对象的整个状态,但您只需要外部可见的内容。您可以通过如下方式创建Gson对象来更改行为:

GsonBuilder().excludeFieldsWithModifiers(Modifier.PRIVATE, Modifier.PROTECTED).create();

什么是地图?它是您的对象吗?您知道您正在将
maple
转换为JSON,对吗?这是一个
MapPlot
,而不是
地质点。我不知道什么是
MapPlot
,但这就是像素坐标的原因。对不起,MapPlot是我的类,它只包含两个地质点。顺便说一句,GsonBuilder位于com.google.gson.GsonBuilder,这里的修饰符是java.lang.reflect.Modifier.interest-我想这些像素可能有一些用处。你能详细说明一下你的“顺便”评论吗?我不明白你的意思。谢谢。因为我提供了一个代码片段(没有导入),所以您无法从中看到我引用的类的位置。这就是全部。