通过微信输入的两个你的位置可以实现测距,测距是调用百度的location business service车联网这个WebService,对于餐馆就可以提供这个功能,甚至是提供路径规划。
public class LocationMessageHandler extends DefaultMessageHandler {
@Override
public boolean canDo(Map<String, String> requestMap) {
// 消息类型
String msgType = requestMap.get("MsgType");
return msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION);
}
@Override
public BaseMessage handleByMe(Map<String, String> requestMap) {
String latitude = requestMap.get("Location_X");//纬度
String longitude = requestMap.get("Location_Y");//经度
int random = new Random().nextInt(4);
/**
* 目前只是测试功能,以后加上数据库之后根据用户的选择进行处理
*/
if(random==0){
//测距,以后可以提供免费送货服务,在距离范围内
int distance = BaiduDistanceService.getDistance(latitude,longitude);
return MessageFactory.createTextMessage(fromUserName, toUserName, "您距离我们大约"+distance+"米");
}else if(random==1){
//周边搜索
String zhoubian = BaiduZhoubianSearchService.searchZhoubian(latitude, longitude, "酒店");
return MessageFactory.createTextMessage(fromUserName, toUserName, zhoubian);
}else if(random==2){
//导航
List<Article> articles = new ArrayList<Article>();
Article article = new Article();
article.setTitle("导航");
article.setDescription("点击此链接导航到我公司");
article.setPicUrl("");//以后放一张公司的宣传照
//高德导航String urlString = "http://mo.amap.com/?from={$latitude},{$longitude}(你的位置)&to=23.378341,116.706653(我的公司)&type=0&opt=1&dev=1";
String url = "http://api.map.baidu.com/direction?origin=latlng:"+latitude+","+longitude+"|name:你的位置&destination=latlng:41.136646,122.066261|name:我的公司&mode=driving®ion=盘锦&output=html&src=yourCompanyName|yourAppName";
article.setUrl(url);
articles.add(article);
return MessageFactory.createNewsMessage(fromUserName, toUserName, articles);
}else{
//百度静态地图导航
String url = "http://api.map.baidu.com/staticimage?width=400&height=300¢er="+longitude+","+latitude+"&zoom=14&markers=122.066261,41.136646|"+longitude+","+latitude+"&markerStyles=l,0|l,1";
List<Article> articles = new ArrayList<Article>();
Article article = new Article();
article.setTitle("导航");
article.setDescription("按地图标注导航到我公司");
article.setPicUrl("");//以后放一张公司的宣传照
article.setUrl(url);
articles.add(article);
return MessageFactory.createNewsMessage(fromUserName, toUserName, articles);
}
}
@Test
public void testHandleMessage(){
Map<String, String> requestMap = new HashMap<String, String>();
requestMap.put("FromUserName", "dfgsd");
requestMap.put("ToUserName", "dfsfd");
requestMap.put("MsgType", MessageUtil.REQ_MESSAGE_TYPE_LOCATION);
requestMap.put("Location_X", "41");
requestMap.put("Location_Y", "122");
BaseMessage message = new LocationMessageHandler().handleMessage(requestMap);
System.out.println(MessageUtil.messageToXml(message));
}
}
/**
* 多店铺分别测距找出最小的就是就近店铺
* google测距http://maps.googleapis.com/maps/api/distancematrix/xml?origins={$latitude},{$longitude}&destinations=23.355164,116.681889&mode=walking&language=zh-CN&sensor=false
* google地理位置http://maps.googleapis.com/maps/api/geocode/xml?latlng={$latitude},{$longitude}&sensor=false&language=zh-CN
* 根据经纬度找位置http://api.map.baidu.com/telematics/v2/reverseGeocoding?location={$longitude},{$latitude}&ak=1a3cde429f38434f1811a75e1a90310c
* @author xsy
*
*/
public class BaiduDistanceService {
//离我宿舍的距离
public static final String url = "http://api.map.baidu.com/telematics/v2/distance?output=json&waypoints=122.066246,41.136665;LONGITUDE,LATITUDE&ak=4Dlcr5PebDTnRuIsv6kChkA5";
@SuppressWarnings("unused")
private static String httpRequest(String requestUrl) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(false);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
} catch (Exception e) {
}
return buffer.toString();
}
public static int getDistance(String latitude,String longitude){
String requestUrl = url.replaceAll("LONGITUDE", longitude)
.replaceAll("LATITUDE", latitude);
System.out.println(requestUrl);
String jsonString = httpRequest(requestUrl);
JSONObject object = JSONObject.fromObject(jsonString);
JSONArray array = object.getJSONArray("results");
int distance = (int) array.getDouble(0);
return distance;
}
@Test
public void testGetDistance(){
System.out.println(String.valueOf(getDistance("210.214511", "124.127465")));
}
}