高德地图地理码和经纬度转化(latlng和latlonpoint)
生活随笔
收集整理的這篇文章主要介紹了
高德地图地理码和经纬度转化(latlng和latlonpoint)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
重寫他的onGeocodeSearched、onRegeocodeSearched的兩個Void的方法來實現地理碼的轉化和逆轉。2、地理碼轉化成經緯度
主要代碼:
// 第一個參數表示地址,第二個參數表示查詢城市,中文或者中文全拼,citycode、adcode,
都不寫則默認所有城市
GeocodeQuery query = new GeocodeQuery(name, null);
geocoderSearch.getFromLocationNameAsyn(query);// 設置同步地理編碼請求地理編碼查詢回調@Overridepublic void onGeocodeSearched(GeocodeResult result, int rCode) {dismissDialog();if (rCode == 1000) {if (result != null && result.getGeocodeAddressList() != null&& result.getGeocodeAddressList().size() > 0) {GeocodeAddress address = result.getGeocodeAddressList().get(0)double dimensionality = address.getLatLonPoint().getLatitude();double longitude = address.getLatLonPoint().getLongitude();LatLng latlng = new LatLng(dimensionality, longitude);aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));geoMarker.setPosition(AMapUtil.convertToLatLng(address.getLatLonPoint()));addressName = "經緯度值:" + address.getLatLonPoint() + "\n位置描述:"+ address.getFormatAddress();ToastUtil.show(GeocoderActivity.this, addressName);} else {ToastUtil.show(GeocoderActivity.this, "shiasa");}} else {ToastUtil.showerror(this, rCode);}}
3、通過經緯度轉化為地理地址LatLonPoint latLonPoint = new LatLonPoint(39.90865, 116.39751);
// 第一個參數表示一個Latlng,第二參數表示范圍多少米,第三個參數表示是火系坐標系還是GPS原生坐標系
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);// 設置同步逆地理編碼請求 geocoderSearch.getFromLocationAsyn(query);逆地理編碼回調
@Overridepublic void onRegeocodeSearched(RegeocodeResult result, int rCode) {dismissDialog();if (rCode == 1000) {if (result != null && result.getRegeocodeAddress() != null&& result.getRegeocodeAddress().getFormatAddress() != null) {addressName = result.getRegeocodeAddress().getFormatAddress()+ "附近";aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(AMapUtil.convertToLatLng(latLonPoint), 15));regeoMarker.setPosition(AMapUtil.convertToLatLng(latLonPoint));ToastUtil.show(GeocoderActivity.this, addressName);} else {ToastUtil.show(GeocoderActivity.this, "shiasa");}} else {ToastUtil.showerror(this, rCode);}}
4、精華一句
latlng=AMapUtil.convertToLatLng(latLonPoint)
AMapUtil 工具類
public class AMapUtil {/*** 判斷edittext是否null*/public static String checkEditText(EditText editText) {if (editText != null && editText.getText() != null&& !(editText.getText().toString().trim().equals(""))) {return editText.getText().toString().trim();} else {return "";}}public static Spanned stringToSpan(String src) {return src == null ? null : Html.fromHtml(src.replace("\n", "<br />"));}public static String colorFont(String src, String color) {StringBuffer strBuf = new StringBuffer();strBuf.append("<font color=").append(color).append(">").append(src).append("</font>");return strBuf.toString();}public static String makeHtmlNewLine() {return "<br />";}public static String makeHtmlSpace(int number) {final String space = "?";StringBuilder result = new StringBuilder();for (int i = 0; i < number; i++) {result.append(space);}return result.toString();}public static String getFriendlyLength(int lenMeter) {if (lenMeter > 10000) // 10 km{int dis = lenMeter / 1000;return dis + "";}if (lenMeter > 1000) {float dis = (float) lenMeter / 1000;DecimalFormat fnum = new DecimalFormat("##0.0");String dstr = fnum.format(dis);return dstr;}if (lenMeter > 100) {int dis = lenMeter / 50 * 50;return dis + "";}int dis = lenMeter / 10 * 10;if (dis == 0) {dis = 10;}return dis + "";}public static boolean IsEmptyOrNullString(String s) {return (s == null) || (s.trim().length() == 0);}/*** 把LatLng對象轉化為LatLonPoint對象*/public static LatLonPoint convertToLatLonPoint(LatLng latlon) {return new LatLonPoint(latlon.latitude, latlon.longitude);}/*** 把LatLonPoint對象轉化為LatLon對象*/public static LatLng convertToLatLng(LatLonPoint latLonPoint) {return new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude());}/*** 把集合體的LatLonPoint轉化為集合體的LatLng*/public static ArrayList<LatLng> convertArrList(List<LatLonPoint> shapes) {ArrayList<LatLng> lineShapes = new ArrayList<LatLng>();for (LatLonPoint point : shapes) {LatLng latLngTemp = AMapUtil.convertToLatLng(point);lineShapes.add(latLngTemp);}return lineShapes;}/*** long類型時間格式化*/public static String convertToTime(long time) {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(time);return df.format(date);}public static final String HtmlBlack = "#000000";public static final String HtmlGray = "#808080";public static String getFriendlyTime(int second) {if (second > 3600) {int hour = second / 3600;int miniate = (second % 3600) / 60;return hour + "小時" + miniate + "分鐘";}if (second >= 60) {int miniate = second / 60;return miniate + "分鐘";}return second + "秒";}public static String getBusPathTitle(BusPath busPath) {if (busPath == null) {return String.valueOf("");}List<BusStep> busSetps = busPath.getSteps();if (busSetps == null) {return String.valueOf("");}StringBuffer sb = new StringBuffer();for (BusStep busStep : busSetps) {if (busStep.getBusLines().size() > 0) {RouteBusLineItem busline = busStep.getBusLines().get(0);if (busline == null) {continue;}String buslineName = getSimpleBusLineName(busline.getBusLineName());sb.append(buslineName);sb.append(" > ");}if (busStep.getRailway() != null) {RouteRailwayItem railway = busStep.getRailway();sb.append(railway.getTrip() + "(" + railway.getDeparturestop().getName()+ " - " + railway.getArrivalstop().getName() + ")");sb.append(" > ");}}return sb.substring(0, sb.length() - 3);}public static String getBusPathDes(BusPath busPath) {if (busPath == null) {return String.valueOf("");}long second = busPath.getDuration();String time = getFriendlyTime((int) second);float subDistance = busPath.getDistance();String subDis = getFriendlyLength((int) subDistance);float walkDistance = busPath.getWalkDistance();String walkDis = getFriendlyLength((int) walkDistance);return String.valueOf(time + " | " + subDis + " | 步行" + walkDis);}public static String getSimpleBusLineName(String busLineName) {if (busLineName == null) {return String.valueOf("");}return busLineName.replaceAll("\\(.*?\\)", "");}}
總結
以上是生活随笔為你收集整理的高德地图地理码和经纬度转化(latlng和latlonpoint)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《电脑音乐制作实战指南:伴奏、录歌、MT
- 下一篇: 容联云通讯短信平台JS调用