百度接口都有了哦
不需要地图,直接调http接口:/**
* 根据城市和详细地址获得地理坐标(经度和纬度)
* @param city 城市, 比如:湖南省怀化市
* @param addr 详细地址, 比如:天星东路1号
* @param key 百度地图的密钥
* @return double[]
*/
public static double[] getPoint(String city, String addr, String key) {
double[] result = new double[2];
try {
if (!isBlank(city) && !isBlank(addr) && !isBlank(key)) {
String response = getSoundCode(BAIDU_GEOCODER + "?city=" + URLEncoder.encode(city, CharEncoding.UTF_8) + "&address=" + URLEncoder.encode(addr, CharEncoding.UTF_8) + "&output=json&ak=" + key, null, null, null, null, false);
if (!isBlank(response)) {
Map<String, Object> map = GsonUtils.fromJson(response, Map.class);
if (((Double) map.get("status")).intValue() == 0) {
Map<String, Object> location = (Map<String, Object>) ((Map<String, Object>) map.get("result")).get("location");
result = new double[] { (double) location.get("lng"), (double) location.get("lat") };
}
}
}
} catch (Exception e) {
log.error("根据城市和详细地址获得地理坐标", e);
}
return result;
}有了地图就更方便了,在监听器里接收数据即可:
private MKSearchListener searchListener = new MKSearchListener() {
// 驾车搜索规划路径
public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
planPathPub(res, error);
}
// 公交搜索规划路径
public void onGetTransitRouteResult(MKTransitRouteResult res, int error) {
planPathPub(res, error);
}
// 步行搜索规划路径
public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {
planPathPub(res, error);
}
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
}
public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1, int arg2) {
}
// 搜索周边资源
public void onGetPoiResult(MKPoiResult res, int type, int error) {
if (null != gressLayout) {gressLayout.setVisibility(View.GONE);}gressLayout = null;
if (error != 0 || null == res) {Toast.makeText(activity, "抱歉,未找到结果", Toast.LENGTH_LONG).show();return;}
if (res.getCurrentNumPois() > 0) {
poiResult = res;
MyPoiOverlay overlay = new MyPoiOverlay(activity, map, search);
overlay.setData(res.getAllPoi());
map.getOverlays().clear();
map.getOverlays().add(overlay);
map.refresh();
for (MKPoiInfo info : res.getAllPoi()) {
if (null != info.pt) {map.getController().animateTo(info.pt);break;}
}
doCallback();
} else if (res.getCityListNum() > 0) {
Toast.makeText(activity, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
}
}
public void onGetPoiDetailSearchResult(int type, int error) {
}
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
}
// 根据地址找坐标或根据坐标找地址
public void onGetAddrResult(MKAddrInfo info, int err) {
if (null != gressLayout) {gressLayout.setVisibility(View.GONE);}gressLayout = null;
if (err != 0) {return;}
if (info.type == MKAddrInfo.MK_GEOCODE) {
/**lz想要的就是这行代码了*/
addrPoint = new double[] { info.geoPt.getLongitudeE6() / 1e6, info.geoPt.getLatitudeE6() / 1e6 };
} else if (info.type == MKAddrInfo.MK_REVERSEGEOCODE) {
addr = info.strAddr;
component = info.addressComponents;
}
setPoints(new GeoPoint[] { info.geoPt }, new String[] { info.strAddr }, new Drawable[] { activity.getResources().getDrawable(R.drawable.icon_f) });
doCallback();
}
};
不需要地图,直接调http接口:/**
* 根据城市和详细地址获得地理坐标(经度和纬度)
* @param city 城市, 比如:湖南省怀化市
* @param addr 详细地址, 比如:天星东路1号
* @param key 百度地图的密钥
* @return double[]
*/
public static double[] getPoint(String city, String addr, String key) {
double[] result = new double[2];
try {
if (!isBlank(city) && !isBlank(addr) && !isBlank(key)) {
String response = getSoundCode(BAIDU_GEOCODER + "?city=" + URLEncoder.encode(city, CharEncoding.UTF_8) + "&address=" + URLEncoder.encode(addr, CharEncoding.UTF_8) + "&output=json&ak=" + key, null, null, null, null, false);
if (!isBlank(response)) {
Map<String, Object> map = GsonUtils.fromJson(response, Map.class);
if (((Double) map.get("status")).intValue() == 0) {
Map<String, Object> location = (Map<String, Object>) ((Map<String, Object>) map.get("result")).get("location");
result = new double[] { (double) location.get("lng"), (double) location.get("lat") };
}
}
}
} catch (Exception e) {
log.error("根据城市和详细地址获得地理坐标", e);
}
return result;
}有了地图就更方便了,在监听器里接收数据即可:
private MKSearchListener searchListener = new MKSearchListener() {
// 驾车搜索规划路径
public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
planPathPub(res, error);
}
// 公交搜索规划路径
public void onGetTransitRouteResult(MKTransitRouteResult res, int error) {
planPathPub(res, error);
}
// 步行搜索规划路径
public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {
planPathPub(res, error);
}
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
}
public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1, int arg2) {
}
// 搜索周边资源
public void onGetPoiResult(MKPoiResult res, int type, int error) {
if (null != gressLayout) {gressLayout.setVisibility(View.GONE);}gressLayout = null;
if (error != 0 || null == res) {Toast.makeText(activity, "抱歉,未找到结果", Toast.LENGTH_LONG).show();return;}
if (res.getCurrentNumPois() > 0) {
poiResult = res;
MyPoiOverlay overlay = new MyPoiOverlay(activity, map, search);
overlay.setData(res.getAllPoi());
map.getOverlays().clear();
map.getOverlays().add(overlay);
map.refresh();
for (MKPoiInfo info : res.getAllPoi()) {
if (null != info.pt) {map.getController().animateTo(info.pt);break;}
}
doCallback();
} else if (res.getCityListNum() > 0) {
Toast.makeText(activity, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
}
}
public void onGetPoiDetailSearchResult(int type, int error) {
}
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
}
// 根据地址找坐标或根据坐标找地址
public void onGetAddrResult(MKAddrInfo info, int err) {
if (null != gressLayout) {gressLayout.setVisibility(View.GONE);}gressLayout = null;
if (err != 0) {return;}
if (info.type == MKAddrInfo.MK_GEOCODE) {
/**lz想要的就是这行代码了*/
addrPoint = new double[] { info.geoPt.getLongitudeE6() / 1e6, info.geoPt.getLatitudeE6() / 1e6 };
} else if (info.type == MKAddrInfo.MK_REVERSEGEOCODE) {
addr = info.strAddr;
component = info.addressComponents;
}
setPoints(new GeoPoint[] { info.geoPt }, new String[] { info.strAddr }, new Drawable[] { activity.getResources().getDrawable(R.drawable.icon_f) });
doCallback();
}
};
所以通过百度地图api或者的坐标不能在google地图上准确定位位置
但如果只是在百度地图sdk可以根据地理位置查找地理坐标