今天把高德地图android sdk搜索附近的功能搞定了。
和定位一样,引入jar,AndroidManifest.xml和定位的一样,没有需要修改的地方。改的多的是Java插件。
原java示例代码中,
query = new PoiSearch.Query("", deepType, "北京市");
poiSearch.setBound(new SearchBound(lp, 2000, true));
poiSearch.setOnPoiSearchListener(this);
这3处在反编译后显示,这几个都是在一个类下面的,而不是自身是一个类,尝试后发现都无法直接在unity中调用,都需要转一下。
这里,不知道是高德的代码不规范,还是unity的方法不给力,还是我的水平太烂,总之,反编译以后显示在类下面的枚举,类,接口都要自己编写java转一下。
另外,这里貌似java中的队列和unity中的队列似乎不太一样,为了省事,也用java转了一下。
新建一个java工程
转方法的类
package com.nsh.amapHelper;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.poisearch.PoiSearch;
public class AMapSearchHelper {
public PoiSearch.Query getPoiSearch(String keyword,String searchType,String cityCode ){
PoiSearch.Query query = new PoiSearch.Query(keyword, searchType, cityCode) ;
return query;
}
public PoiSearch.SearchBound getBound(LatLonPoint lp,int radiusInMeters,boolean isDistanceSort){
PoiSearch.SearchBound bound = new PoiSearch.SearchBound(lp,radiusInMeters,isDistanceSort);
return bound;
}
}
转接口的类
package com.nsh.amapHelper;
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener;
public abstract interface AMapListenerHelper extends OnPoiSearchListener{
}
重新解析结果的类,出口经纬度不一定有。
package com.nsh.amapHelper;
import java.util.ArrayList;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
public class AMapPoiResultHelper {
private PoiResult poiResult;
private ArrayList<PoiItem> poiItems;
public void setPoiResult(PoiResult result){
poiResult = result;
poiItems = poiResult.getPois();
}
public int poiItemNumber(){
if (poiItems != null && poiItems.size() > 0){
return poiItems.size();
}else{
return 0;
}
}
public String poinItemInfo(int i){
String strReturn= "";
if(i>-1&&i<poiItems.size()){
// strReturn = strReturn+"||行政区划代码>"+poiItems.get(i).getAdCode();
// strReturn = strReturn+"||行政区划名称>"+poiItems.get(i).getAdName();
// strReturn = strReturn+"||所在商圈>"+poiItems.get(i).getBusinessArea();
// strReturn = strReturn+"||城市编码>"+poiItems.get(i).getCityCode();
// strReturn = strReturn+"||getDirection>"+poiItems.get(i).getDirection();
// strReturn = strReturn+"||距离中心点的距离>"+poiItems.get(i).getDistance();
// strReturn = strReturn+"||电子邮件>"+poiItems.get(i).getEmail();
// strReturn = strReturn+"||停车场类型>"+poiItems.get(i).getParkingType();
// strReturn = strReturn+"||唯一标识>"+poiItems.get(i).getPoiId();
// strReturn = strReturn+"||邮编>"+poiItems.get(i).getPostcode();
// strReturn = strReturn+"||行政区编码>"+poiItems.get(i).getProvinceCode();
// strReturn = strReturn+"||政区名称>"+poiItems.get(i).getProvinceName();
// strReturn = strReturn+"||地址>"+poiItems.get(i).getSnippet();
// strReturn = strReturn+"||电话>"+poiItems.get(i).getTel();
strReturn = strReturn+"||名称>"+poiItems.get(i).getTitle();
// strReturn = strReturn+"||类型描述>"+poiItems.get(i).getTypeDes();
// strReturn = strReturn+"||网址>"+poiItems.get(i).getWebsite();
//strReturn = strReturn+"||" +poiItems.get(i).getLatLonPoint().getLatitude();
try{
strReturn = strReturn+"||经纬度坐标>"
+poiItems.get(i).getLatLonPoint().getLatitude()
+"\\"+poiItems.get(i).getLatLonPoint().getLongitude();
strReturn = strReturn+"||入口坐标>"
+poiItems.get(i).getEnter().getLatitude()
+"\\"+poiItems.get(i).getLatLonPoint().getLongitude();
strReturn = strReturn+"||出口坐标>"
+poiItems.get(i).getExit().getLatitude()
+"\\"+poiItems.get(i).getLatLonPoint().getLongitude();
}catch(Exception ex){
strReturn = strReturn +"----"+ex.getMessage();
}
}
return strReturn;
}
}
java打包编译成jar后引入。
untiy部分
将java事件转为unity事件
using UnityEngine;
using System.Collections;
public class SearchEvent : AndroidJavaProxy {
public SearchEvent ()
: base ("com.nsh.amapHelper.AMapListenerHelper")
{
}
void onPoiSearched (AndroidJavaObject result,int rCode)
{
if (poiSearched != null) {
poiSearched (result,rCode);
}
}
void onPoiItemSearched(AndroidJavaObject paramPoiItem,int paramInt){
}
public delegate void DelegateOnPoiSearched(AndroidJavaObject result,int rCode);
public event DelegateOnPoiSearched poiSearched;
}
unity调用方法,这里没有实时获取当前定位,直接手写了一个。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class SearchManage : MonoBehaviour
{
public Text txtInfo;
public Text txtPoi;
public InputField inputQuery;
private SearchEvent searchEvent;
private AndroidJavaClass jcu;
private AndroidJavaObject jou;
private AndroidJavaObject query;
private AndroidJavaObject poiSearch;
private AndroidJavaObject lastPoint;
public void StartSearch ()
{
try {
txtInfo.text = "start search...";
txtInfo.text = txtInfo.text + "\r\n";
jcu = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
jou = jcu.GetStatic<AndroidJavaObject> ("currentActivity");
txtInfo.text = txtInfo.text + "currentActivity get...";
txtInfo.text = txtInfo.text + "\r\n";
AndroidJavaObject amapHelper = new AndroidJavaObject("com.nsh.amapHelper.AMapSearchHelper");
txtInfo.text = txtInfo.text + "helper get...";
txtInfo.text = txtInfo.text + "\r\n";
AndroidJavaObject query = amapHelper.Call<AndroidJavaObject>("getPoiSearch",inputQuery.text,"生活服务","0871");
txtInfo.text = txtInfo.text + "query get...";
txtInfo.text = txtInfo.text + "\r\n";
query.Call ("setPageSize", 20);
query.Call ("setPageNum", 1);
txtInfo.text = txtInfo.text + "page set...";
txtInfo.text = txtInfo.text + "\r\n";
poiSearch = new AndroidJavaObject ("com.amap.api.services.poisearch.PoiSearch", jou, query);
txtInfo.text = txtInfo.text + "poiSearch set...";
txtInfo.text = txtInfo.text + "\r\n";
lastPoint = new AndroidJavaObject ("com.amap.api.services.core.LatLonPoint", 25.054777d, 102.671163d);
txtInfo.text = txtInfo.text + "lastPoint set...";
txtInfo.text = txtInfo.text + "\r\n";
AndroidJavaObject bound = amapHelper.Call<AndroidJavaObject>("getBound",lastPoint,1000,true);
txtInfo.text = txtInfo.text + "bound set...";
txtInfo.text = txtInfo.text + "\r\n";
poiSearch.Call ("setBound", bound);
txtInfo.text = txtInfo.text + "setBound set...";
txtInfo.text = txtInfo.text + "\r\n";
searchEvent = new SearchEvent ();
searchEvent.poiSearched += OnPoiSearched;
txtInfo.text = txtInfo.text +"searchEvent set...";
poiSearch.Call ("setOnPoiSearchListener", searchEvent);
txtInfo.text = txtInfo.text + "setOnPoiSearchListener set...";
txtInfo.text = txtInfo.text + "\r\n";
poiSearch.Call ("searchPOIAsyn");
txtInfo.text = txtInfo.text + "searchPOIAsyn set...";
} catch (Exception ex) {
txtInfo.text = txtInfo.text + "\r\n--------------------\r\n";
txtInfo.text = txtInfo.text + ex.Message;
}
}
private void OnPoiSearched (AndroidJavaObject result, int rCode)
{
try {
if (rCode == 0) {
if (result != null && result.Call<AndroidJavaObject> ("getQuery") != null) {// 搜索poi的结果
txtPoi.text = "reslut get.";
txtPoi.text = txtPoi.text +">>";
txtPoi.text = txtPoi.text+"页面数:"+result.Call<int>("getPageCount").ToString();
txtPoi.text = txtPoi.text +">>";
AndroidJavaObject resultHelper = new AndroidJavaObject("com.nsh.amapHelper.AMapPoiResultHelper");
txtPoi.text = txtPoi.text +"resulthelper get..";
resultHelper.Call("setPoiResult",result);
txtPoi.text = txtPoi.text+"resultHelper set...";
int num =resultHelper.Call<int>("poiItemNumber");
txtPoi.text = txtPoi.text +">>记录数:"+num.ToString()+">>";
for(int i=0;i<num;i++){
txtPoi.text = txtPoi.text +"\r\n";
txtPoi.text = txtPoi.text + resultHelper.Call<String>("poinItemInfo",i);
}
} else {
txtPoi.text = "没有找到相关数据。";
}
} else if (rCode == 27) {
txtPoi.text = "搜索失败,请检查网络连接。";
} else if (rCode == 32) {
txtPoi.text = "key验证无效。";
} else {
txtPoi.text = "未知错误,请稍后重试。错误代码:" + rCode.ToString ();
}
} catch (Exception ex) {
txtPoi.text = txtPoi.text + "\r\n----------------\r\n";
txtPoi.text = txtPoi.text + ex.Message;
}
}
}
编译以后运行效果