最近一个项目有用到地图的需求,我是通过一个AMapWrapper的回调来获取到Amap对象,跟官方的实例中mapview.getMap()不太一样,然后在回调方法中再给这个amap设置locationstyle,也就是是否显示定位蓝点等等,定位是正确的,也都能定位成功,但是无论怎么设置,蓝点就是不显示,还有一系列的uisetting也是不显示(放大放小,回到当前位置那些按钮),有知道的好兄弟可以帮忙看看吗?下面有两个核心代码,一个是LocationFragment,一个是自定义的一个MyWebWrapper。
一,LoacationFragment
public class LocationFragment extends Fragment implements LocationSource.OnLocationChangedListener, AMap.OnMapClickListener, AMap.InfoWindowAdapter, View.OnTouchListener,AMapLocationListener {
//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
//声明AMapLocationClientOption对象
public AMapLocationClientOption mLocationOption = null;
LocationSource.OnLocationChangedListener mListener;
public AMapLocation location;
public AMap map;
private WebView webview;
View infoWindow = null;
private NestedScrollView scrollView;
private LinearLayout layout;
private LinearLayout layoutBtn;
private MapView mapView;
//初始化定位
//设置定位回调监听
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_location, container, false);
initView(rootView);
initLocate();
initMap();
initEvent();
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
setUpMap(savedInstanceState);
return rootView;
}
private void initEvent() {
webview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
}
});
layoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), DetailActivity.class);
startActivity(intent);
}
});
}
private void initView(View rootView) {
webview = rootView.findViewById(R.id.webview);
scrollView = rootView.findViewById(R.id.pull_down_layout);
scrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
layout = rootView.findViewById(R.id.pull_down_linearlayout);
/* layout.setNestedScrollingEnabled(false);
scrollView.setNestedScrollingEnabled(false);
scrollView.setScrollbarFadingEnabled(true);*/
layoutBtn = rootView.findViewById(R.id.pull_down_layout_plant);
}
private MarkerOptions MarkerOptionsManager(LatLng latLng, String name) {
MarkerOptions markerOption = new MarkerOptions();
markerOption.position(latLng);
// markerOption.title(name).snippet("植物");
markerOption.draggable(true);//设置Marker可拖动
markerOption.icon(BitmapDescriptorFactory.fromView(getActivity().getLayoutInflater().inflate(R.layout.marker_icon,null)));
// 将Marker设置为贴地显示,可以双指下拉地图查看效果
// markerOption.setFlat(true);//设置marker平贴地图效果
markerOption.setGps(true);
return markerOption;
}
private MarkerOptions MarkerOptionsManager2(LatLng latLng,String name) {
MarkerOptions markerOption = new MarkerOptions();
markerOption.position(latLng);
// markerOption.title(name).snippet("植物");
markerOption.draggable(true);//设置Marker可拖动
markerOption.icon(BitmapDescriptorFactory.fromView(getActivity().getLayoutInflater().inflate(R.layout.marker_icon,null)));
// 将Marker设置为贴地显示,可以双指下拉地图查看效果
// markerOption.setFlat(true);//设置marker平贴地图效果
markerOption.setGps(true);
return markerOption;
}
private void initMarker(AMap aMap) {
LatLng latLng = new LatLng(21.150661067659183,110.3071524442981);
LatLng latLng1 = new LatLng(21.157661067459986,110.30111524442982);
LatLng latLng2 = new LatLng(21.158661067259482,110.30611524442983);
LatLng latLng3 = new LatLng(21.151661067151281,110.30511524442984);
LatLng latLng4 = new LatLng(21.159661067959285,110.30211524442985);
MarkerOptions markerOptions = MarkerOptionsManager(latLng, "枫香树");
MarkerOptions markerOptions1 = MarkerOptionsManager2(latLng1, "食人花");
MarkerOptions markerOptions2 = MarkerOptionsManager(latLng2, "豌豆射手");
MarkerOptions markerOptions3 = MarkerOptionsManager(latLng3, "樱桃炸弹");
MarkerOptions markerOptions4 = MarkerOptionsManager(latLng4, "窝瓜");
ArrayList<MarkerOptions> list = new ArrayList<>();
list.add(markerOptions);
list.add(markerOptions1);
list.add(markerOptions2);
list.add(markerOptions3);
list.add(markerOptions4);
Marker marker1 = aMap.addMarker(markerOptions1);
marker1.setAnchor(44f,54f);
marker1.showInfoWindow();
Marker marker = aMap.addMarker(markerOptions4);
marker.showInfoWindow();
aMap.setInfoWindowAdapter(this);
aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// marker.showInfoWindow();
scrollView.fullScroll(NestedScrollView.FOCUS_UP);
return true;
}
});
aMap.addMarkers(list,true);
}
private void initMap() {
MyLocationStyle locationStyle;
locationStyle = new MyLocationStyle();//初始化定位蓝点样式
// locationStyle.interval(2000);//设置连续定位模式下的的定位间隔,值在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒
locationStyle.showMyLocation(true);
locationStyle.strokeColor(Color.BLUE);
locationStyle.radiusFillColor(Color.RED);
locationStyle.strokeWidth(20);
locationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.gps_point));
locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);
locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);
MAWebViewWrapper webViewWrapper = new MAWebViewWrapper(webview);
AMapWrapper aMapWrapper = new AMapWrapper(getContext(),webViewWrapper);
aMapWrapper.onCreate();
aMapWrapper.getMapAsyn(new AMap.OnMapReadyListener() {
@Override
public void onMapReady(AMap aMap) {
map=aMap;
initMarker(aMap);
aMap.setMyLocationEnabled(true);//设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false
aMap.setMyLocationStyle(locationStyle); //设置定位蓝点的style
aMap.getUiSettings().setAllGesturesEnabled(true); // 设置默认定位按钮是否显示,非必须设置
locationStyle.anchor(0.0f,1.0f);
locationStyle.showMyLocation(true);
aMap.setMyLocationStyle(locationStyle);
}
});
}
private void getLocation(AMapLocation location) {
this.location=location;
}
private void setUpMap(Bundle savedInstanceState) {
}
//声明定位回调监听器
AMapLocationListener mLocationListener =new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation location) {
getLocation(location);
map.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
map.moveCamera(CameraUpdateFactory.zoomTo(16));
if (null != location) {
System.out.println("错误信息————"+ location.getErrorInfo());
StringBuffer sb = new StringBuffer();
//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
if(location.getErrorCode() == 0){
sb.append("定位成功" + "\n");
sb.append("定位类型: " + location.getLocationType() + "\n");
sb.append("经 度 : " + location.getLongitude() + "\n");
sb.append("纬 度 : " + location.getLatitude() + "\n");
sb.append("精 度 : " + location.getAccuracy() + "米" + "\n");
sb.append("提供者 : " + location.getProvider() + "\n");
sb.append("速 度 : " + location.getSpeed() + "米/秒" + "\n");
sb.append("角 度 : " + location.getBearing() + "\n");
// 获取当前提供定位服务的卫星个数
sb.append("星 数 : " + location.getSatellites() + "\n");
sb.append("国 家 : " + location.getCountry() + "\n");
sb.append("省 : " + location.getProvince() + "\n");
sb.append("市 : " + location.getCity() + "\n");
sb.append("城市编码 : " + location.getCityCode() + "\n");
sb.append("区 : " + location.getDistrict() + "\n");
sb.append("区域 码 : " + location.getAdCode() + "\n");
sb.append("地 址 : " + location.getAddress() + "\n");
sb.append("兴趣点 : " + location.getPoiName() + "\n");
//定位完成的时间
// sb.append("定位时间: " + Utils.formatUTC(location.getTime(), "yyyy-MM-dd HH:mm:ss") + "\n");
} else {
//定位失败
sb.append("定位失败" + "\n");
sb.append("错误码:" + location.getErrorCode() + "\n");
sb.append("错误信息:" + location.getErrorInfo() + "\n");
sb.append("错误描述:" + location.getLocationDetail() + "\n");
}
sb.append("***定位质量报告***").append("\n");
sb.append("* WIFI开关:").append(location.getLocationQualityReport().isWifiAble() ? "开启":"关闭").append("\n");
sb.append("* GPS状态:").append(getGPSStatusString(location.getLocationQualityReport().getGPSStatus())).append("\n");
sb.append("* GPS星数:").append(location.getLocationQualityReport().getGPSSatellites()).append("\n");
sb.append("* 网络类型:" + location.getLocationQualityReport().getNetworkType()).append("\n");
sb.append("* 网络耗时:" + location.getLocationQualityReport().getNetUseTime()).append("\n");
sb.append("****************").append("\n");
//定位之后的回调时间
// sb.append("回调时间: " + Utils.formatUTC(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "\n");
//解析定位结果,
String result = sb.toString();
System.out.println(result);
// tvResult.setText(result);
} else {
System.out.println("定位失败");
// tvResult.setText("定位失败,loc is null");
}
}
};
private String getGPSStatusString(int statusCode){
String str = "";
switch (statusCode){
case AMapLocationQualityReport.GPS_STATUS_OK:
str = "GPS状态正常";
break;
case AMapLocationQualityReport.GPS_STATUS_NOGPSPROVIDER:
str = "手机中没有GPS Provider,无法进行GPS定位";
break;
case AMapLocationQualityReport.GPS_STATUS_OFF:
str = "GPS关闭,建议开启GPS,提高定位质量";
break;
case AMapLocationQualityReport.GPS_STATUS_MODE_SAVING:
str = "选择的定位模式中不包含GPS定位,建议选择包含GPS定位的模式,提高定位质量";
break;
case AMapLocationQualityReport.GPS_STATUS_NOGPSPERMISSION:
str = "没有GPS定位权限,建议开启gps定位权限";
break;
}
return str;
}
private void initLocate() {
//初始化AMapLocationClientOption对象
//mLocationOption = new AMapLocationClientOption();
mLocationOption = getDefaultOption();
mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
if(null != mLocationClient){
mLocationClient.setLocationOption(mLocationOption);
}//设置场景模式后最好调用一次stop,再调用start以保证场景模式生效 locationClient.stopLocation(); locationClient.startLocation();
try {
AMapLocationClient.updatePrivacyShow(getContext(),true,true);
AMapLocationClient.updatePrivacyAgree(getContext(),true);
mLocationClient = new AMapLocationClient(getContext());
} catch (Exception e) {
e.printStackTrace();
}
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.setLocationListener(mLocationListener);
mLocationClient.startLocation();
}
private AMapLocationClientOption getDefaultOption(){
AMapLocationClientOption mOption = new AMapLocationClientOption();
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
mOption.setInterval(2000);//可选,设置定位间隔。默认为2秒
mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false
mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
mOption.setGeoLanguage(AMapLocationClientOption.GeoLanguage.DEFAULT);//可选,设置逆地理信息的语言,默认值为默认语言(根据所在地区选择语言)
return mOption;
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation!=null&&mListener!=null){
mListener.onLocationChanged(aMapLocation);
mLocationListener.onLocationChanged(aMapLocation);
System.out.println("显示系统小兰点——————————————————");
}else {
System.out.println("定位失败");
}
}
@Override
public void onMapClick(LatLng latLng) {
}
@Override
public void onLocationChanged(Location location) {
if (mLocationListener!=null&&mListener!=null){
mListener.onLocationChanged(location);
System.out.println("显示系统小兰点——————————————————");
}else {
System.out.println("定位失败");
}
}
@Override
public View getInfoWindow(Marker marker) {
if(infoWindow == null) {
infoWindow = LayoutInflater.from(getContext()).inflate(
R.layout.info_window_layout, null);
}
// render(marker, infoWindow);
return infoWindow;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
scrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
return true;
}
}
二、MAWebViewWrapper
public class MAWebViewWrapper implements IAMapWebView {
private final WebView webView;
private WebViewClient mapWebViewClient;
public MAWebViewWrapper(final WebView webView) {
this.webView = webView;
if (this.webView != null) {
this.webView.setWebViewClient(
new WebViewClient() {
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (mapWebViewClient != null) {
boolean flag = mapWebViewClient.shouldOverrideUrlLoading(view, request);
if (flag) {
return true;
}
}
return super.shouldOverrideUrlLoading(view, request);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if (mapWebViewClient != null) {
WebResourceResponse flag = mapWebViewClient.shouldInterceptRequest(view, request);
if (flag != null) {
return flag;
}
}
return super.shouldInterceptRequest(view, request);
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (mapWebViewClient != null) {
WebResourceResponse flag = mapWebViewClient.shouldInterceptRequest(view, url);
if (flag != null) {
return flag;
}
}
return super.shouldInterceptRequest(view, url);
}
}
);
}
}
@Override
public void evaluateJavascript(String jsCallSig, ValueCallback<String> callback) {
if (this.webView != null) {
this.webView.evaluateJavascript(jsCallSig, callback);
}
}
@Override
public void loadUrl(String toString) {
if (this.webView != null) {
this.webView.loadUrl(toString);
}
}
@SuppressLint("JavascriptInterface")
@Override
public void addAMapJavascriptInterface(IAMapJsCallback object, String javascriptInterfaceName) {
if (this.webView != null) {
this.webView.addJavascriptInterface(object, javascriptInterfaceName);
}
}
@Override
public void setWebViewClient(WebViewClient webViewClient) {
this.mapWebViewClient = webViewClient;
}
@Override
public int getWidth() {
if (this.webView != null) {
return this.webView.getWidth();
}
return 0;
}
@Override
public int getHeight() {
if (this.webView != null) {
this.webView.getHeight();
}
return 0;
}
@Override
public void addView(View v, ViewGroup.LayoutParams params) {
if (webView != null && v != null) {
webView.addView(v, params);
}
}
}
我都是直接复制demo的代码过来的,没有出现你过你说的这个问题。你先跑一下demo,然后demo是正常的话,你一步一步去读他们的代码,然后拷贝过来你自己使用。