android高德地图调用定位显示

注意:本程序是我亲自测过的,可以支持直接下载使用

不同的eclipse需要注册申请不同的高德地图key,否则不能显示地图,你们自己下载

下载地址:http://download.csdn.net/detail/cf8833/9272283

package com.example.zzmap;

import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnCameraChangeListener;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.LocationSource.OnLocationChangedListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.Projection;
import com.amap.api.maps.model.BitmapDescriptorFactory;
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.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeRoad;
import com.amap.api.services.geocoder.StreetNumber;


public class LocationActivity extends Activity {
private AMap aMap;
private MapView mapView;
private OnLocationChangedListener mListener;
private LocationManagerProxy mAMapLocationManager;
private Marker locationMarker;
private LatLng locationLatLng;
public static Drawable mapPrintScreen;
private Handler handler = new Handler();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState); //此处必须加上
init();
}


private void init() {
if (aMap == null) {
aMap = mapView.getMap();
if (AMapUtil.checkReady(this, aMap)) {
setUpMap();
}
}
}


private void setUpMap() {
aMap.setOnCameraChangeListener(cameraChangeListener);
aMap.setInfoWindowAdapter(infoWindowAdapter);
mAMapLocationManager = LocationManagerProxy
.getInstance(LocationActivity.this);
aMap.setLocationSource(locationSource);
aMap.setMyLocationEnabled(true);// 设置为true表示系统定位按钮显示并响应点击,false表示隐藏,默认是false
aMap.getUiSettings().setMyLocationButtonEnabled(false);
aMap.getUiSettings().setTiltGesturesEnabled(false);
}


/**
* 往地图上添加marker

* @param latLng
*/
private void addMarker(LatLng latLng, String desc) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("[我的位置]");
markerOptions.snippet(desc);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker());
locationMarker = aMap.addMarker(markerOptions);
}


 


LocationSource locationSource = new LocationSource() {


@Override
public void deactivate() {
mListener = null;
if (mAMapLocationManager != null) {
mAMapLocationManager.removeUpdates(aMapLocationListener);
mAMapLocationManager.destroy();
}
mAMapLocationManager = null;
}


@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
if (mAMapLocationManager == null) {
mAMapLocationManager = LocationManagerProxy
.getInstance(LocationActivity.this);
}
 
mAMapLocationManager.requestLocationData(
LocationProviderProxy.AMapNetwork, 5000, 10,
aMapLocationListener);
}
};


AMapLocationListener aMapLocationListener = new AMapLocationListener() {


@Override
public void onLocationChanged(AMapLocation aLocation) {
if (mListener != null) {
// 此处注释掉,表示不用系统提供的定位图标等
// mListener.onLocationChanged(aLocation);
}
if (aLocation != null) {
Double geoLat = aLocation.getLatitude();
Double geoLng = aLocation.getLongitude();
locationLatLng = new LatLng(geoLat, geoLng);


String desc = "";
Bundle locBundle = aLocation.getExtras();
if (locBundle != null) {
desc = locBundle.getString("desc");
}
addMarker(locationLatLng, desc);
locationMarker.showInfoWindow();// 显示信息窗口
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
locationLatLng, 15));
locationSource.deactivate();
 
}
}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}


@Override
public void onProviderEnabled(String provider) {
}


@Override
public void onProviderDisabled(String provider) {
}


@Override
public void onLocationChanged(Location location) {
}


};


OnCameraChangeListener cameraChangeListener = new OnCameraChangeListener() {


@Override
public void onCameraChangeFinish(CameraPosition position) {
if (locationMarker != null) {


final LatLng latLng = position.target;
new Thread(new Runnable()
{

@Override
public void run()
{
GeocodeSearch geocodeSearch = new GeocodeSearch(LocationActivity.this);
LatLonPoint point =new LatLonPoint(latLng.latitude, latLng.longitude);
RegeocodeQuery regeocodeQuery = new RegeocodeQuery(point, 1000,GeocodeSearch.AMAP);
RegeocodeAddress address = null;
try {
address = geocodeSearch.getFromLocation(regeocodeQuery);
} catch (AMapException e) {
e.printStackTrace();
}
if(null==address){
return;
}
StringBuffer stringBuffer = new StringBuffer();
String area = address.getProvince();//省或直辖市
String loc = address.getCity();//地级市或直辖市
String subLoc = address.getDistrict();//区或县或县级市
String ts = address.getTownship();//乡镇
String thf = null;//道路
List<RegeocodeRoad> regeocodeRoads = address.getRoads();//道路列表
if(regeocodeRoads != null && regeocodeRoads.size() > 0)
{
RegeocodeRoad regeocodeRoad = regeocodeRoads.get(0);
if(regeocodeRoad != null)
{
thf = regeocodeRoad.getName();
}
}
String subthf = null;//门牌号
StreetNumber streetNumber = address.getStreetNumber();
if(streetNumber != null)
{
subthf = streetNumber.getNumber();
}
String fn = address.getBuilding();//标志性建筑,当道路为null时显示
if (area != null)
stringBuffer.append(area);
if(loc!=null&&!area.equals(loc))
stringBuffer.append(loc);
if (subLoc != null)
stringBuffer.append(subLoc);
if (ts != null)
stringBuffer.append(ts);
if (thf != null)
stringBuffer.append(thf);
if (subthf != null)
stringBuffer.append(subthf);
if ((thf == null && subthf == null) && fn != null&&!subLoc.equals(fn))
stringBuffer.append(fn + "附近");
locationMarker.setSnippet(stringBuffer.toString());
handler.post(new Runnable()
{

@Override
public void run()
{
locationMarker.showInfoWindow();
}
});
}
}).start();



}
}


@Override
public void onCameraChange(CameraPosition position) {
if (locationMarker != null) {
LatLng latLng = position.target;
locationMarker.setPosition(latLng);
}
}
};


InfoWindowAdapter infoWindowAdapter = new InfoWindowAdapter() {


@Override
public View getInfoWindow(Marker marker) {
return null;
}


@Override
public View getInfoContents(Marker marker) {
View mContents = getLayoutInflater().inflate(R.layout.custom_info_contents,
null);
render(marker, mContents);
return mContents;
}
};

class GetLocationTask extends AsyncTask<Object, Integer, Object>{


@Override
protected Object doInBackground(Object... params) {
return null;
}

@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);


}

}


/** 自定义infowindow的样式 */
public void render(Marker marker, View view) {
String title = marker.getTitle();
TextView titleUi = ((TextView) view.findViewById(R.id.title));
if (title != null) {
titleUi.setText(title);
} else {
titleUi.setText("");
}
String snippet = marker.getSnippet();
TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
if (snippet != null) {
snippetUi.setText(snippet);
} else {
snippetUi.setText("");
}
}


/**
* marker点击时跳动一下
*/
public void jumpPoint(final Marker marker, final LatLng latLng) {


final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = aMap.getProjection();
Point startPoint = proj.toScreenLocation(latLng);
startPoint.offset(0, -100);
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 1500;


final Interpolator interpolator = new BounceInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * latLng.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * latLng.latitude + (1 - t)
* startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
handler.postDelayed(this, 16);
}
}
});
}


/**
* 把一个view转化成bitmap对象
*/
public static Bitmap getViewBitmap(View view) {
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}


/**
* 把一个xml布局文件转化成view
*/
public View getView(String title, String text) {
View view = getLayoutInflater().inflate(R.layout.marker, null);
// TextView text_title = (TextView)
// view.findViewById(R.id.marker_title);
//textViewLocationInfo = (TextView) view.findViewById(R.id.marker_text);
// text_title.setText(title);
//textViewLocationInfo.setText(text);
return view;
}


/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}


/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
locationSource.deactivate();
mapView.onPause();
}

/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

/**
* 方法必须重写
*/
@Override
protected void onDestroy()
{
super.onDestroy();
mapView.onDestroy();
}


}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 高德地图SDK的蓝点显示通常由定位功能控制。如果您使用高德地图SDK时发现蓝点未显示,可以按照以下步骤进行排查和解决: 1. 确认是否已添加定位权限:在AndroidManifest.xml文件中添加权限:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />。 2. 确认是否已经调用了AMapLocationClient的startLocation()方法开始定位,并且实现了AMapLocationListener接口来处理定位结果。 3. 检查是否已经为AMap对象设置了MyLocationStyle样式,并将其设置为显示蓝点:AMap.setMyLocationStyle(MyLocationStyle.showMyLocation(true))。 4. 确保设备已开启位置服务:在手机设置中前往"位置"(或"定位服务")菜单,确保位置服务已打开,并允许应用程序进行定位。 5. 确认是否检查了定位权限:可以调用方法`checkSelfPermission()`检查是否已授权位置权限,并在未授权时请求权限。 6. 检查是否已在AndroidManifest.xml文件中注册了`AMapBroadcastReceiver`广播接收器和相应的Action。 7. 检查是否设置了地图控件的定位按钮,并为其设置了点击事件。 如果以上步骤都已正确配置,但仍然无法显示蓝点,可能是由于其他因素造成的问题,可能需要检查您的代码逻辑是否有其他冲突或错误。如果问题仍然存在,建议检查高德地图SDK的版本和更新日志,查看是否有已知的蓝点显示问题,并在高德地图SDK官方论坛或社区寻求帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值