我只是想知道为什么当我尝试时,代码在同一个地方给了我很多标记 获取用户当前位置?
如果有人知道如何在服务或AsyncTask中执行此操作,我无法在不同的类服务中执行此操作,这将对我的项目有很大帮助。
谢谢!
package toutel.testcarte;
import org.osmdroid.ResourceProxy;
import org.osmdroid.bonuspack.overlays.Marker;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
private MapController mapController;
private MapView mapView;
private GeoPoint myposition;
private ItemizedOverlay<OverlayItem> mMyLocationOverlay;
private ResourceProxy mResourceProxy;
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
mapController = (MapController) mapView.getController();
GeoPoint myposition = new GeoPoint(48.856614, 2.3522219000000177);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setBuiltInZoomControls(false);
mapView.setMultiTouchControls(true);
// mapController.animateTo(myposition);
mapController.setZoom(13);
mapController.setCenter(myposition);
Context context = getApplicationContext();
CharSequence text = "Activez votre GPS pour utiliser la localisation ";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case (R.id.fermer): {
System.exit(0);
break;
}
case (R.id.position): {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
while (true) {
LocationListener mylocationlistener = new LocationListener() {
public void onLocationChanged(Location location) {
Context ctx = getApplicationContext();
GeoPoint myposition = new GeoPoint(
location.getLatitude(), location.getLongitude());
mapController.animateTo(myposition);
mapController.setCenter(myposition);
mapController.setZoom(17);
Marker marker = new Marker(mapView);
marker.setPosition(myposition);
marker.setAnchor(Marker.ANCHOR_CENTER,
Marker.ANCHOR_BOTTOM);
mapView.getOverlays().add(marker);
mapView.invalidate();
try {
Thread.sleep(10 * 1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, mylocationlistener);
break;
}
}
case (R.id.itineraire): {
}
}
return false;
}
}
答案 0 :(得分:0)
Multiple markers : why?
您将获得多个标记,因为每次更改位置时,您都会在地图上放置一个标记。即使位置变化太小。您正在放置标记。尝试放大您将看到所有标记都不在同样的立场。
我的建议在这里不需要服务。因为OnlocationChanged监听器自动完成你的工作。我已经编写了这个单一的类。现在在项目中你需要GPS坐标,只需实例化类对象并获得Latitude和经度。
LocationSender loc = new LocationSender(YourClass.this);
当我将lat,lng(在LocationSender.java中)声明为静态时,我可以获得其值的任何位置。
double latitude = LocationSender.lat;
double longitude = LocationSender.lng;
LocationSender.java
public class LocationSender {
Context context;
LocationManager locationManager;
Location location;
static double lat,lng;
public LocationSender(Context ctx) {
context=ctx;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
// Acquire a reference to the system Location Manager
public void getNewLocation(Location location) throws JSONException
{
String latLongString = "";
if (location != null) {
this.location=location;
lat= location.getLatitude();
lng = location.getLongitude();
latLongString = "Lat:" + String.valueOf(LocationActivity.lat) + "\nLong:" + String.valueOf(LocationActivity.lng);
Log.d("Location Found", latLongString);
} else
{
location=null;
latLongString = "No location found";
}
Toast.makeText(context, latLongString, Toast.LENGTH_LONG).show();
}
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
try {
getNewLocation(location);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onProviderEnabled(String provider) {
Toast.makeText(context, "Provider: "+provider+" : Enabled", Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(context, "Provider: "+provider+" : disabled", Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(context, "Provider: "+provider+" : status: "+status, Toast.LENGTH_LONG).show();
}
};
}
另外,请不要忘记在AndroidManifest.xml中添加权限
答案 1 :(得分:0)
您真的需要针对位置管理员/位置服务的onLocationChanged()
回调进行自定义实施吗?
否则,您可以查看OSMDroid的MyLocationNewOverlay类https://code.google.com/p/osmdroid/source/browse/trunk/osmdroid-android/src/main/java/org/osmdroid/views/overlay/mylocation/MyLocationNewOverlay.java?r=1220
还有一个名为MyLocationOverlay https://code.google.com/p/osmdroid/source/browse/trunk/osmdroid-android/src/org/osmdroid/views/overlay/MyLocationOverlay.java?r=902
的实现(不幸弃用)在我的实现中,我使用它:
//declare variable; global - because I need reference elsewhere
private MyLocationOverlay self;
//somewhere in code
//'this' - is the context
//mapMainView - my mapView
self = new MyLocationOverlay(this, mapMainView);
self.enableMyLocation();
self.enableFollowLocation();
//add the overlay to the map and refresh the view
mapMainView.getOverlays().add(COUNT+1, self);//COUNT is the size of the List<Overlay> returned by mapMainView.getOverlays(); this is just because I want my 'self' to be always the last overlay added
mapMainView.invalidate();//refresh the view
这会在地图上的某个位置放置一个默认图标,并在我移动时不断更新。