Android高德地圖定位、點標記、POI

來源:互聯網
上載者:User

高德地圖API5.0版本以後定位藍點的顯示不依賴於定位API。

package com.example.dell.a3dmap;import android.Manifest;import android.content.Context;import android.content.pm.PackageManager;import android.location.Geocoder;import android.location.Location;import android.location.LocationManager;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;import com.amap.api.location.AMapLocation;import com.amap.api.location.AMapLocationClient;import com.amap.api.maps.AMap;import com.amap.api.maps.AMapUtils;import com.amap.api.maps.LocationSource;import com.amap.api.maps.MapView;import com.amap.api.maps.model.CameraPosition;import com.amap.api.maps.model.LatLng;import com.amap.api.maps.model.Marker;import com.amap.api.maps.model.MarkerOptions;import com.amap.api.maps.model.MyLocationStyle;import com.amap.api.maps.model.Poi;import com.amap.api.services.core.LatLonPoint;import com.amap.api.services.core.PoiItem;import com.amap.api.services.geocoder.GeocodeQuery;import com.amap.api.services.geocoder.GeocodeResult;import com.amap.api.services.geocoder.GeocodeSearch;import com.amap.api.services.geocoder.RegeocodeQuery;import com.amap.api.services.geocoder.RegeocodeResult;import java.util.List;public class MainActivity extends AppCompatActivity implements AMap.OnMyLocationChangeListener {    private MapView mapView;    AMap amap;    MyLocationStyle myLocationStyle;    Marker[] marker = new Marker[10];    int totalMarker;    int WRITE_COARSE_LOCATION_REQUEST_CODE;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化地圖控制項        mapView = (MapView) findViewById(R.id.map);        //必須要寫        mapView.onCreate(savedInstanceState);        totalMarker = 0;        amap = mapView.getMap();        myLocationStyle = new MyLocationStyle();        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);        amap.setMyLocationStyle(myLocationStyle);        amap.getUiSettings().setMyLocationButtonEnabled(true);        amap.setMyLocationEnabled(true);        amap.setOnMyLocationChangeListener(this);        amap.setOnMapLongClickListener(new AMap.OnMapLongClickListener() {            @Override            public void onMapLongClick(final LatLng latLng) {                LatLng mylatlng;                mylatlng = new LatLng(amap.getMyLocation().getLatitude(), amap.getMyLocation().getLongitude());                float dis = AMapUtils.calculateLineDistance(mylatlng, latLng);                marker[totalMarker] = amap.addMarker(new MarkerOptions().position(latLng).title("").snippet("直線距離:" + dis));                final GeocodeSearch geocodeSearch = new GeocodeSearch(getApplicationContext());                geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {                    @Override                    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {                        if (i == 0) {                            System.out.println("i=0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");                        } else {                            List<PoiItem> poiItemList;                            poiItemList = regeocodeResult.getRegeocodeAddress().getPois();                            marker[totalMarker].setTitle(regeocodeResult.getRegeocodeAddress().getDistrict() + getNearestName(poiItemList, latLng) + "附近");                            //System.out.println(regeocodeResult.getRegeocodeAddress().getCity()+"<<<<<<<<<<<<<<<<<<");                            //System.out.println(regeocodeResult.getRegeocodeAddress().getDistrict()+"<<<<<<<<<<<<<<<<<<<<");                            totalMarker++;                        }                    }                    @Override                    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {                    }                });                LatLonPoint latLonPoint = new LatLonPoint(latLng.latitude, latLng.longitude);                RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 500, GeocodeSearch.AMAP);                geocodeSearch.getFromLocationAsyn(query);            }        });        amap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {            @Override            public boolean onMarkerClick(Marker marker) {                if (!marker.isInfoWindowShown()) {                    marker.showInfoWindow();                } else {                    marker.hideInfoWindow();                }                return true;            }        });    }    /**     * 方法必須重寫     */    @Override    protected void onResume() {        super.onResume();        mapView.onResume();    }    /**     * 方法必須重寫     */    @Override    protected void onPause() {        super.onPause();        mapView.onPause();    }    /**     * 方法必須重寫     */    @Override    protected void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        mapView.onSaveInstanceState(outState);    }    /**     * 方法必須重寫     */    @Override    protected void onDestroy() {        super.onDestroy();        mapView.onDestroy();    }    @Override    public void onMyLocationChange(Location location) {        LatLng mylatlng;        mylatlng = new LatLng(location.getLatitude(), location.getLongitude());        for (int i = 0; i <= totalMarker; i++) {            float dis = AMapUtils.calculateLineDistance(mylatlng, marker[i].getPosition());            marker[i].setSnippet("直線距離:" + String.valueOf(dis));        }    }    public String getNearestName(List<PoiItem> poiItemList, LatLng targetLocation) {        double minDis = 500, nowDis;        String ret = "";        for (int i = 0; i <= poiItemList.size() - 1; i++) {            PoiItem poiItem;            poiItem = poiItemList.get(i);            LatLng poilatlng = new LatLng(poiItem.getLatLonPoint().getLatitude(), poiItem.getLatLonPoint().getLongitude());            nowDis = AMapUtils.calculateLineDistance(targetLocation, poilatlng);            if (nowDis < minDis) {                minDis = nowDis;                ret = poiItem.toString();            }        }        return ret;    }}

上面代碼實現了顯示地圖,顯示定位,長按地圖上的某一個點繪製點標記,點標記的資訊裡麵包含了當前位置距離點標記的距離,已經最近了POI地理描述資訊。

由於regeocodeResult.getRegeocodeAddress().getPois()函數返回的是一個POI列表,查詢的是一定範圍內(比如這裡的500米)的所有POI,要返回一個最近的作為地址描述資訊,於是在getNearestName()方法中進行處理,第一個參數是待處理列表,第二個參數是使用者點擊的位置,將列表中的所有點和點擊的位置計算距離,選出最小的那一個作為POI描述資訊。

我覺得這些天研究高德地圖的API主要收穫時學會了閱讀官方文檔,在沒有視頻等直觀學習的方法的情況下,官方文檔是唯一的參考,但也是最好的參考。以上的具體方法和屬性都可以在高德的文檔中找到詳細描述。

效果如下(是不是暴露了我的位置Orz):




 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.