底层更新了一个方法,但是这次让我传一个
map<string, vector<Object>>
这种类型的对象回到上层,刚开始的时候,我是拒绝的。于是乎,立马研读C++里map,vector的遍历,以及编写jni层HashMap的实例化+put的代码。所以目前要作的事就是这3步。
// Aries: 将负责类型map<string, vector<Object>> 转换成上层的HashMap
jobject GetMap(JNIEnv * env, map<string, vector<m2::PointD>> const & pointsMap)
{
LOGD( "TanTu--JNI-------start---");
jobject HashMap = NULL;
if(pointsMap.size() != 0){
LOGD( "TanTu--JNI--------pointsMap.size()===%d", (int)pointsMap.size());
jclass class_hashmap = env->FindClass("java/util/HashMap");
jmethodID hashmap_init = env->GetMethodID(class_hashmap, "<init>",
"()V");
HashMap = env->NewObject(class_hashmap, hashmap_init, "");
jmethodID HashMap_put = env->GetMethodID(class_hashmap, "put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
map<string, vector<m2::PointD>>::const_iterator it;
LOGD( "TanTu--JNI--------start fro===");
for (it = pointsMap.begin(); it != pointsMap.end(); ++it)
{
jstring str = ToJavaString(env, it->first);
// LOGD( "TanTu--JNI--------str===");
// LOGD( "TanTu--JNI--------str===%s", str);
vector<m2::PointD> vecPoint = it->second;
int count = vecPoint.size();
jobjectArray infos = NULL; // jobjectArray 为指针类型
jclass klass = NULL; // jclass 为指针类型
jobject obj;
jmethodID methodID;
klass = env->FindClass("com/mapswithme/maps/bookmarks/data/MapObject$MyPosition");
ASSERT ( klass, () );
methodID = env->GetMethodID(
klass, "<init>",
"(DD)V");
ASSERT ( methodID, () );
infos = env->NewObjectArray(count, klass, NULL);
for (int i = 0; i < count;i++)
{
obj = env->NewObject(klass, methodID,
static_cast<jdouble>(vecPoint[i].y),
static_cast<jdouble>(vecPoint[i].x));
// LOGD( "TanTu--JNI--------NewObject===%d", i);
env->SetObjectArrayElement(infos, i, obj);
}
env->CallObjectMethod(HashMap, HashMap_put, str, infos);
}
// LOGD( "TanTu--JNI--------end=if==");
}
LOGD( "TanTu--JNI--------end===");
return HashMap;
}