gpt4 book ai didi

android - Arcgis : how to get device location

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:08 24 4
gpt4 key购买 nike

嘿,我试图在我的应用程序中实现这些代码,但它不起作用,我不知道我哪里出错了。

基本上,当我启动设备位置示例时。它没有显示我当前所在的位置,也没有看到任何与我当前所在位置相似的蓝点。

我唯一看到的就是 map 。只是一个普通的缩小 map 。

如果有人可以帮助我了解如何使用 map 上显示的蓝点获取当前位置,我将非常感激。

这是我的 MainActivity.class

public class HelloWorld extends Activity {
MapView mMapView = null;
ArcGISTiledMapServiceLayer tileLayer;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


// Retrieve the map and initial extent from XML layout


mMapView = (MapView) findViewById(R.id.map);

mMapView.addLayer(new ArcGISTiledMapServiceLayer(
"http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"));

mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {

public void onStatusChanged(Object source, STATUS status) {
if (source == mMapView && status == STATUS.INITIALIZED) {
LocationService ls = mMapView.getLocationService();
ls.setAutoPan(false);

ls.start();
}

}

});



}


protected void onPause() {
super.onPause();
mMapView.pause();
}

@Override
protected void onResume() {
super.onResume();
mMapView.unpause();
}

}

最佳答案

这是一个代码,每 1 秒通过提供商和 GPS 绘制我的位置。让我们先声明变量:

private GraphicsLayer myGraphicalLayer;
MapView mMapView;
ArcGISLocalTiledLayer baseLayer;
private LocationManager mlocManager;
private LocationListener mlocListener;

在 onCreate 函数中我们调用 LocationListener:

mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mlocListener);
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, mlocListener);
// loading the map
mMapView = (MapView) findViewById(R.id.localMap);
baseLayer = new ArcGISLocalTiledLayer(basemapurl);
mMapView.addLayer(baseLayer);
// defining my position layer
myGraphicalLayer = new GraphicsLayer();

然后是绘制我的位置的函数:

private void SetMyLocationPoint(final double x, final double y) {
PictureMarkerSymbol myPin = new PictureMarkerSymbol(getResources().getDrawable(
R.drawable.mylocation_icon));

Point wgspoint = new Point(x, y);
Point mapPoint = (Point) GeometryEngine.project(wgspoint, SpatialReference.create(4326),
mMapView.getSpatialReference());

Graphic myPinGraphic = new Graphic(mapPoint, myPin);

try {
myGraphicalLayer.removeAll();
} catch (Exception e) {
e.printStackTrace();
}

myGraphicalLayer.addGraphic(myPinGraphic);
myGraphicalLayer.setVisible(true);
mMapView.addLayer(myGraphicalLayer);

}

创建实现 MyLocationListener 的内部类来获取即时位置,并让它像这样调用名为 SetMyLocationPoint 的函数:

public class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location loc) {
SetMyLocationPoint(loc.getLongitude(), loc.getLatitude());
}

@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "provider enabled", Toast.LENGTH_SHORT)
.show();
}

@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "provider disabled", Toast.LENGTH_SHORT)
.show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}

关于android - Arcgis : how to get device location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20062552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com