如何从android应用程序中的数据库中显示地图中的纬度和经度



在double中声明的纬度和经度
错误为:java.lang.NullPointerException:试图在null对象引用上调用虚拟方法"double java.lang.double.doubleValue((">
错误行为:LatLng doctors=new LatLng(纬度,经度(;

contactList = new ArrayList<>();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
new GetContacts().execute();
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng doctors = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(doctors).title(dName));
mMap.moveCamera(CameraUpdateFactory.newLatLng(doctors));
}

从数据库中提取数据
正确提取数据没有问题

private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MapsActivity.this,"Json Data is downloading",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "http://amitumi.com/public/api/doctor/all";
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("data");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String lat = c.getString("lat");
String lon = c.getString("long");
latitude = Double.parseDouble(c.getString(lat));
longitude = Double.parseDouble(c.getString(lon));
dName = c.getString(name);
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("lat", lat);
contact.put("long", lon);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
}

首先,检查您的纬度和经度值,该值应为null,这就是导致此错误的原因。在LatLng内部使用之前,还要对这些值添加一个null检查。

其次,即使这是一个不同的问题,但正如您所提到的,您仍然会遇到一些解析错误,那么为什么不使用Gson来解析响应json字符串呢。这可能会帮助您解决此错误。试试看。

最新更新