我正在尝试使用以下代码将我的GPS位置添加到地图中:
MyLocationNewOverlay locationOverlay;
locationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(mContext), mapView);
locationOverlay.enableMyLocation();
mapView.getOverlays().add(locationOverlay);
但是,代码没有效果,并且没有添加任何内容(例如,没有图标,没有文本等)。请注意,缩放叠加和手势叠加效果很好,但是当我将它们添加到地图视图时,MyLocationNewOverlay叠加层无效。有任何想法吗?
更新:
这是我活动的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, LocationListener {
private static final int STORAGE_REQUEST = 100;
private static final int LOCATION_REQUEST = 101;
private static final String TAG = "MainActivity";
private Context mContext;
private MapView mMap;
private EditText latitude;
private EditText longitude;
private Button searchLocation;
IMapController mapController;
private MyLocationNewOverlay locationOverlay;
ArrayList<OverlayItem> items = new ArrayList<>();
/**
* Compass overlay
*/
private CompassOverlay mCompassOverlay;
private RotationGestureOverlay mRotationOverlay;
private ScaleBarOverlay mScaleBarOverlay;
private MyLocationNewOverlay mMyLocationOverlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getApplicationContext();
Configuration.getInstance().load(mContext, PreferenceManager.getDefaultSharedPreferences(mContext));
Configuration.getInstance().setUserAgentValue("Dalvik");
setContentView(R.layout.activity_main);
//Check Permissions
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_REQUEST);
}
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST);
}
mMap = findViewById(R.id.mapView);
mMap.setTileSource(TileSourceFactory.MAPNIK);
mMap.setBuiltInZoomControls(true);
mMap.setMultiTouchControls(true);
latitude = findViewById(R.id.latitude);
longitude = findViewById(R.id.longitude);
searchLocation = findViewById(R.id.searchLocation);
searchLocation.setOnClickListener(this);
mapController = mMap.getController();
mapController.setZoom(9.0f);
//Add my location
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
final LocationManager locMgr = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
final String provider = locMgr.getBestProvider(criteria, true);
locMgr.requestLocationUpdates(provider, 1000, 100, this);
Location loc = locMgr.getLastKnownLocation(provider);
Log.d(TAG, "onCreate: " + loc.getLatitude() + ", " + loc.getLongitude());
//Start on Iran
GeoPoint iran = new GeoPoint(32.4279, 53.6880);
mapController.setCenter(iran);
//Add my location overlay
mMyLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(mContext), mMap);
mMyLocationOverlay.enableMyLocation();
mMap.getOverlays().add(mMyLocationOverlay);
//Add compass overlay
mCompassOverlay = new CompassOverlay(mContext, mMap);
mCompassOverlay.enableCompass();
mCompassOverlay.setEnabled(true);
mMap.getOverlays().add(mCompassOverlay);
//Add rotation gesture overlay
mRotationOverlay = new RotationGestureOverlay(mMap);
mRotationOverlay.setEnabled(true);
mMap.setMultiTouchControls(true);
mMap.getOverlays().add(mRotationOverlay);
//Add map scale bar overlay
mScaleBarOverlay = new ScaleBarOverlay(mMap);
mScaleBarOverlay.setCentred(true);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mScaleBarOverlay.setScaleBarOffset(dm.widthPixels / 2, 30);
mMap.getOverlays().add(mScaleBarOverlay);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//Check and decide
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(mContext, R.string.permission_not_granted_please_allow, Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.searchLocation) {
double lat = Double.valueOf(latitude.getText().toString());
double lng = Double.valueOf(longitude.getText().toString());
GeoPoint geoPoint = new GeoPoint(lat, lng);
mapController.setCenter(geoPoint);
}
}
@Override
protected void onPause() {
super.onPause();
mMyLocationOverlay.setEnabled(false);
mMap.onPause();
}
@Override
protected void onResume() {
super.onResume();
mMyLocationOverlay.setEnabled(true);
mMap.onResume();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
答案 0 :(得分:1)
这被问了很多。这是不工作的最常见原因
如果您碰巧有自定义位置提供商或需要自定义叠加层使用的位置Feed,请尝试以下
GpsMyLocationProvider prov= new GpsMyLocationProvider();
prov.addLocationSource(LocationManager.NETWORK_PROVIDER);
MyLocationNewOverlay locationOverlay = new MyLocationNewOverlay
locationOverlay(prov, mapView);
locationOverlay.enableMyLocation();
mMap.getOverlayManager().add(locationOverlay);
然后照常进行。
答案 1 :(得分:0)
您似乎无法使用GpsMyLocationProvider
向地图添加叠加层。更好的方法可能是使用ItemizedIconOverlay
,您可以使用Android getLocationUpdates
从Android获取位置,创建GeoPoint,创建ItemOverlay
,将其添加到ItemizedIconOverlay
,然后将其添加到MapView:
final LocationManager locMgr = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
final String provider = locMgr.getBestProvider(criteria, true);
locMgr.requestLocationUpdates(provider, 1000, 100, this);
Location loc = locMgr.getLastKnownLocation(provider);
//Add my location overlay
OverlayItem newItem = new OverlayItem("Here", "You are here", new GeoPoint(loc));
items.add(newItem);
ItemizedIconOverlay<OverlayItem> myLocOverlay = new ItemizedIconOverlay<OverlayItem>(items,
getResources().getDrawable(R.drawable.my_location, null),
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(int index, OverlayItem item) {
return false;
}
@Override
public boolean onItemLongPress(int index, OverlayItem item) {
return false;
}
},
mContext);
mMap.getOverlays().add(myLocOverlay);