ArcGIS for Android实现地图加载、放大缩小及定位功能
- java代码
package com.map.mapgis;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ZoomControls;
import java.util.List;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.PictureMarkerSymbol;
public class MapgisActivity extends Activity {
Button GPS_btn;
MapView mMapView ;
ArcGISTiledMapServiceLayer tileLayer;
Point point;
Point wgspoint;
GraphicsLayer gLayerPos;
PictureMarkerSymbol locationSymbol;
LocationManager locMag;
Location loc ;
Point mapPoint;
TextView txtview;
ZoomControls zoomCtrl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMapView = (MapView)findViewById(R.id.map);
tileLayer = new ArcGISTiledMapServiceLayer(
"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer");
mMapView.addLayer(tileLayer);
zoomCtrl=(ZoomControls)findViewById(R.id.zoomCtrl);
zoomCtrl.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mMapView.zoomin();
}
});
zoomCtrl.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mMapView.zoomout();
}
});
gLayerPos = new GraphicsLayer();
mMapView.addLayer(gLayerPos);
locationSymbol = new PictureMarkerSymbol(this.getResources().getDrawable(R.drawable.location));
locMag = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//获取所有可用的位置提供器
GPS_btn=(Button)findViewById(R.id.GPS_btn);
GPS_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
final List<String> providers=locMag.getProviders(true);
for(String provider:providers)
{
loc = locMag.getLastKnownLocation(provider);
LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location location) {
markLocation(location);
}
public void onProviderDisabled(String arg0)
{
}
public void onProviderEnabled(String arg0)
{
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2)
{
}
};
locMag.requestLocationUpdates(provider, 2000, 10, locationListener);
if(loc!=null)
{
markLocation(loc);
}
}
}
});
}
private void markLocation(Location location)
{
gLayerPos.removeAll();
double locx = location.getLongitude();//经度
double locy = location.getLatitude();//纬度
wgspoint = new Point(locx, locy);
//定位到所在位置
mapPoint = (Point) GeometryEngine.project(wgspoint,SpatialReference.create(4326),mMapView.getSpatialReference());
Graphic graphic = new Graphic(mapPoint,locationSymbol);
gLayerPos.addGraphic(graphic);
mMapView.centerAt(mapPoint, true);
mMapView.setScale(100);
mMapView.setMaxResolution(300);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
mMapView.pause();
}
@Override protected void onResume() {
super.onResume();
mMapView.unpause();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- MapView layout and initial extent -->
<com.esri.android.map.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
initExtent = "-20037507.0672, -30240971.9584, 20037507.0672, 30240971.9584">
</com.esri.android.map.MapView>
<ZoomControls
android:id="@+id/zoomCtrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15.0dip"
android:layout_marginRight="5.0dip"/>
<Button
android:id="@+id/GPS_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="27.0dip"
android:layout_marginLeft="10.0dip"
android:background="@drawable/gps_btn_selector"
/>
</RelativeLayout>
- 定位按钮xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/ic_location" android:state_pressed="false"/>
<item android:drawable="@drawable/ic_location_press" android:state_pressed="true"/>
</selector>
- AndroidManifest.xml添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
如图,效果还是可以的~精度不是很高,模拟器运行要实现定位需要给模拟器发送一个经纬度才行
附上代码连接,有需要的可以下载看看
Arcgis For Android定位功能源码