
我使用LocationManager跟踪用户当前位置.现在,在将位置管理器更改为FusedLocation API之后,即使在设置map.setMyLocationEnabled(true)后,也不会显示蓝点和圆圈.我可以在地图片段的右上角看到当前位置图标,但点击它不会做任何事情.我将我的代码恢复到LocationManager现在我能够看到指向我当前位置的蓝点.使用Fused Location API可能有什么问题.
解决方法:
用于针对API-23或更高版本
见this answer….
对于API-22和更低的目标:
此代码适用于我,它具有MyLocation蓝点/圆,并且它还使用融合位置提供程序在当前位置放置标记.
这是我使用的整个活动代码:
import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import com.Google.androID.gms.maps.GoogleMap;import com.Google.androID.gms.maps.SupportMapFragment;import androID.location.Location;import androID.Widget.Toast;import com.Google.androID.gms.common.ConnectionResult;import com.Google.androID.gms.common.API.Googleapiclient;import com.Google.androID.gms.location.LocationRequest;import com.Google.androID.gms.location.LocationServices;import com.Google.androID.gms.location.LocationListener;import com.Google.androID.gms.maps.model.BitmapDescriptorFactory;import com.Google.androID.gms.maps.model.LatLng;import com.Google.androID.gms.maps.model.Marker;import com.Google.androID.gms.maps.model.MarkerOptions;import com.Google.androID.gms.maps.OnMapReadyCallback;public class MainActivity extends AppCompatActivity implements Googleapiclient.ConnectionCallbacks, Googleapiclient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback { LocationRequest mLocationRequest; Googleapiclient mGoogleapiclient; LatLng latLng; GoogleMap mGoogleMap; SupportMapFragment mFragment; Marker mCurrLocation; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByID(R.ID.map); mFragment.getMapAsync(this); } @OverrIDe public voID onMapReady(GoogleMap GoogleMap) { mGoogleMap = GoogleMap; mGoogleMap.setMyLocationEnabled(true); buildGoogleapiclient(); mGoogleapiclient.connect(); } @OverrIDe public voID onPause() { super.onPause(); //Unregister for location callbacks: if (mGoogleapiclient != null) { LocationServices.FusedLocationAPI.removeLocationUpdates(mGoogleapiclient, this); } } protected synchronized voID buildGoogleapiclient() { Toast.makeText(this,"buildGoogleapiclient",Toast.LENGTH_SHORT).show(); mGoogleapiclient = new Googleapiclient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addAPI(LocationServices.API) .build(); } @OverrIDe public voID onConnected(Bundle bundle) { Toast.makeText(this,"onConnected",Toast.LENGTH_SHORT).show(); Location mLastLocation = LocationServices.FusedLocationAPI.getLastLocation( mGoogleapiclient); if (mLastLocation != null) { //place marker at current position mGoogleMap.clear(); latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()); MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.Title("Current position"); markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_magenta)); mCurrLocation = mGoogleMap.addMarker(markerOptions); } mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(5000); //5 seconds mLocationRequest.setFastestInterval(3000); //3 seconds mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); //mLocationRequest.setSmallestdisplacement(0.1F); //1/10 meter LocationServices.FusedLocationAPI.requestLocationUpdates(mGoogleapiclient, mLocationRequest, this); } @OverrIDe public voID onConnectionSuspended(int i) { Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onConnectionFailed(ConnectionResult connectionResult) { Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onLocationChanged(Location location) { //remove prevIoUs current location marker and add new one at current position if (mCurrLocation != null) { mCurrLocation.remove(); } latLng = new LatLng(location.getLatitude(), location.getLongitude()); MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.Title("Current position"); markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_magenta)); mCurrLocation = mGoogleMap.addMarker(markerOptions); Toast.makeText(this,"Location Changed",Toast.LENGTH_SHORT).show(); //If you only need one location, unregister the Listener //LocationServices.FusedLocationAPI.removeLocationUpdates(mGoogleapiclient, this); }}activity_main.xml中:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" androID:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <fragment androID:ID="@+ID/map" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"/></relativeLayout>结果:
总结以上是内存溢出为你收集整理的使用android融合位置api在MyLocation上不显示蓝点和圆圈全部内容,希望文章能够帮你解决使用android融合位置api在MyLocation上不显示蓝点和圆圈所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)