Java 多重标记:为什么?
我只是想知道为什么当我尝试去做的时候,代码会在同一个地方给我很多标记 获取用户当前位置 如果有人知道如何在服务或异步任务中做到这一点,那么我没有在不同的类服务中做到这一点,这将对我的项目有很大帮助 谢谢Java 多重标记:为什么?,java,android,maps,osmdroid,Java,Android,Maps,Osmdroid,我只是想知道为什么当我尝试去做的时候,代码会在同一个地方给我很多标记 获取用户当前位置 如果有人知道如何在服务或异步任务中做到这一点,那么我没有在不同的类服务中做到这一点,这将对我的项目有很大帮助 谢谢 package toutel.testcarte; import org.osmdroid.ResourceProxy; import org.osmdroid.bonuspack.overlays.Marker; import org.osmdroid.tileprovider.tileso
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;
}
}
package toutel.testcarte;
导入org.osmdroid.ResourceProxy;
导入org.osmdroid.bonuspack.overlays.Marker;
导入org.osmdroid.tileprovider.tilesource.TileSourceFactory;
导入org.osmdroid.util.GeoPoint;
导入org.osmdroid.views.MapController;
导入org.osmdroid.views.MapView;
导入org.osmdroid.views.overlay.ItemizedOverlay;
导入org.osmdroid.views.overlay.OverlayItem;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
专用地图控制器;
私有地图视图;
私人地质点位置;
私有项目覆盖mMyLocationOverlay;
私有资源代理mResourceProxy;
@SuppressLint(“新API”)
@抑制警告(“弃用”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView=(mapView)findViewById(R.id.mapView);
mapController=(mapController)mapView.getController();
地质点myposition=新地质点(48.856614,2.35222190000000177);
setTileSource(TileSourceFactory.MAPNIK);
mapView.SetBuilTinZoomControl(假);
设置多点触控控件(真);
//mapController.animateTo(我的位置);
mapController.setZoom(13);
mapController.setCenter(我的位置);
Context=getApplicationContext();
CharSequence text=“Activez votre GPS倾倒利用器la定位”;
int duration=Toast.LENGTH\u LONG;
Toast Toast=Toast.makeText(上下文、文本、持续时间);
toast.show();
}
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
公共布尔值OnOptionItemSelected(菜单项菜单){
开关(menu.getItemId()){
案例(R.id.fermer):{
系统出口(0);
打破
}
案例(R.id.位置):{
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
while(true){
LocationListener mylocationlistener=新LocationListener(){
已更改位置上的公共无效(位置){
Context ctx=getApplicationContext();
地质点myposition=新地质点(
location.getLatitude(),location.getLatitude());
mapController.animateTo(我的位置);
mapController.setCenter(我的位置);
mapController.setZoom(17);
标记=新标记(地图视图);
标记器。设置位置(myposition);
marker.setAnchor(marker.ANCHOR_中心、,
标记。锚(底部);
mapView.getOverlays().add(标记);
mapView.invalidate();
试一试{
线程。睡眠(10*1000);
}
捕捉(中断异常e){
e、 printStackTrace();
}
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
//TODO自动生成的方法存根
}
};
lm.RequestLocationUpdate(LocationManager.GPS_提供程序,10000,0,mylocationlistener);
打破
}
}
案例(R.id.I):{
}
}
返回false;
}
}
您将获得多个标记,因为每次更改位置时,您都会在地图上放置一个标记。即使位置的更改太小。您正在放置标记。尝试放大时,您将看到所有标记都不在同一位置
我的建议是这里不需要服务。因为OnlocationChanged listener会自动完成您的工作。我已经编写了一个类。现在在项目中需要GPS坐标的地方,只需实例化类对象并获取纬度和经度
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中添加权限您真的需要位置管理器/位置服务的
onLocationChanged()回调的自定义实现吗
否则,您可以看看OSMDroid的MyLocationNewOverlay类
还有另一个实现(不推荐的unfortunatelly),称为MyLocationOverlay
在我的实现中,我是这样使用它的:
//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
//声明变量;全球-因为
//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