高德地图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 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 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;
}
}
由于regeocodeResult.getRegeocodeAddress().getPois()函数返回的是一个POI列表,查询的是一定范围内(比如这里的500米)的所有POI,要返回一个最近的作为地址描述信息,于是在getNearestName()方法中进行处理,第一个参数是待处理列表,第二个参数是用户点击的位置,将列表中的所有点和点击的位置计算距离,选出最小的那一个作为POI描述信息。
我觉得这些天研究高德地图的API主要收获时学会了阅读官方文档,在没有视频等直观学习的方法的情况下,官方文档是唯一的参考,但也是最好的参考。以上的具体方法和属性都可以在高德的文档中找到详细描述。
效果如下(是不是暴露了我的位置Orz):