Java tutorial
package com.tsroad.map; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Color; import android.location.Location; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationListener; import com.amap.api.location.LocationManagerProxy; import com.amap.api.location.LocationProviderProxy; import com.amap.api.maps.AMap; import com.amap.api.maps.LocationSource; import com.amap.api.maps.MapView; import com.amap.api.maps.model.BitmapDescriptor; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.CircleOptions; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.MyLocationStyle; import java.util.ArrayList; /** % @authors Keung Charteris & T.s.road CZQ % @version 1.0 ($Revision$) % @addr. GUET, Gui Lin, 540001, P.R.China % @contact : cztsiang@gmail.com % @date Copyright(c) 2016-2020, All rights reserved. % This is an open access code distributed under the Creative Commons Attribution License, which permits % unrestricted use, distribution, and reproduction in any medium, provided the original work is properly cited. * Created by tsroad on 01/05/15. */ public class SetFenceActivity extends FragmentActivity implements AMap.OnMapClickListener, AMapLocationListener, GestureDetector.OnGestureListener, LocationSource { private MapView mapView = null; private LocationManagerProxy mLocationManagerProxy = null; private String GEOFENCE_BROADCAST_ACTION = "com.tsroad.map"; private PendingIntent mPendingIntent = null; private AMap aMap = null; private AMap mMap = null; private LocationSource.OnLocationChangedListener mListener; private LocationManagerProxy mAMapLocationManager; private Marker marker;// ?? private Button setConfirmButton = null; //??2 private double FenceRadius = 2000; private EditText FenceRadiusInput = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.set_fence); mapView = (MapView) findViewById(R.id.set_center_fence); mapView.onCreate(savedInstanceState); //? mMap = mapView.getMap(); mMap.setOnMapClickListener(this); // setConfirmButton = (Button) findViewById(R.id.set_confirm_button); setConfirmButton.setOnClickListener(new ConfirmButtonListener()); //? FenceRadiusInput = (EditText) findViewById(R.id.set_radius_fence); //? mLocationManagerProxy = LocationManagerProxy.getInstance(this); //?? Intent intent = new Intent(GEOFENCE_BROADCAST_ACTION); mPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(GEOFENCE_BROADCAST_ACTION); this.registerReceiver(mSetFenceReceiver, intentFilter); mLocationManagerProxy.requestLocationData(LocationProviderProxy.AMapNetwork, 2000, 15, this); init(); deactivate(); } //? class ConfirmButtonListener implements View.OnClickListener { @Override public void onClick(View v) { FenceRadius = Double.parseDouble(FenceRadiusInput.getText().toString()); Toast.makeText(SetFenceActivity.this, "??" + FenceRadiusInput.getText().toString() + "" + "\n??", Toast.LENGTH_SHORT).show(); } } @Override protected void onStart() { super.onStart(); Toast.makeText(SetFenceActivity.this, "??", Toast.LENGTH_SHORT).show(); } private BroadcastReceiver mSetFenceReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int i = intent.getIntExtra("status", -1); Log.e("-----------------", ""); if (1 == i) Toast.makeText(SetFenceActivity.this, "?", Toast.LENGTH_SHORT).show(); else if (0 == i) Toast.makeText(SetFenceActivity.this, "?", Toast.LENGTH_SHORT).show(); } }; // /** // * ? // */ // @Override // protected void onSaveInstanceState(Bundle outState) { // super.onSaveInstanceState(outState); // mapView.onSaveInstanceState(outState); // } @Override protected void onResume() { super.onResume(); mapView.onResume(); } @Override protected void onDestroy() { super.onDestroy(); this.unregisterReceiver(mSetFenceReceiver); mapView.onDestroy(); deactivate(); } private int clickFlag = 0; @Override public void onMapClick(LatLng latLng) { deactivate(); mMap.clear(); SetUpMap2(); // if(clickFlag<=1) { Log.e("--------------------", "lat:" + latLng.latitude); Log.e("--------------------", "lon:" + latLng.longitude); mMap.addCircle(new CircleOptions().center(latLng).radius(FenceRadius).strokeColor(Color.RED)); Toast.makeText(SetFenceActivity.this, "??:" + latLng.toString(), Toast.LENGTH_SHORT) .show(); //??? mLocationManagerProxy.addGeoFenceAlert(latLng.latitude, latLng.longitude, (float) FenceRadius, 1000 * 60 * 30, mPendingIntent); } } @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) { } @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; } //------------------------------------------------------------ //------------------------------------------------------------ //-------------------------??----------------------- //------------------------------------------------------------ //------------------------------------------------------------ //------------------------------------------------------------ /** * ?? */ private void init() { if (aMap == null) { aMap = mapView.getMap(); setUpMap(); } // Toast.makeText(SetFenceActivity.this, "GPS", Toast.LENGTH_LONG).show(); } private void setUpMap() { SetUpMap2(); aMap.setLocationSource(this);// ?? aMap.getUiSettings().setMyLocationButtonEnabled(true);// ?? aMap.setMyLocationEnabled(true);// true????false???????false //??? ????????? aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE); } private void SetUpMap2() { ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>(); giflist.add(BitmapDescriptorFactory.fromResource(R.drawable.point1)); giflist.add(BitmapDescriptorFactory.fromResource(R.drawable.point2)); giflist.add(BitmapDescriptorFactory.fromResource(R.drawable.point3)); giflist.add(BitmapDescriptorFactory.fromResource(R.drawable.point4)); giflist.add(BitmapDescriptorFactory.fromResource(R.drawable.point5)); // giflist.add(BitmapDescriptorFactory.fromResource(R.drawable.point6)); marker = mapView.getMap().addMarker(new MarkerOptions().anchor(0.5f, 0.5f).icons(giflist).period(50)); // ??? MyLocationStyle myLocationStyle = new MyLocationStyle(); // myLocationStyle.myLocationIcon(BitmapDescriptorFactory // .fromResource(R.drawable.location_marker));// ?? myLocationStyle.strokeColor(Color.BLACK);// myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// // myLocationStyle.anchor(int,int)//?? myLocationStyle.strokeWidth(0.1f);// aMap.setMyLocationStyle(myLocationStyle); // aMap.setMyLocationRotateAngle(180); aMap.getUiSettings().setCompassEnabled(true);// ?? } /** * ? */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } AMapLocation aLocation = null; /** * ??? */ @Override public void onLocationChanged(AMapLocation aLocation) { if (mListener != null && aLocation != null) { mListener.onLocationChanged(aLocation);// ?? marker.setPosition(new LatLng(aLocation.getLatitude(), aLocation.getLongitude()));// ?? float bearing = aMap.getCameraPosition().bearing; aMap.setMyLocationRotateAngle(bearing);// ?? aMap.getMinZoomLevel(); if (this.aLocation == null || this.aLocation.getStreet() != aLocation.getStreet()) { this.aLocation = aLocation; LatLng latng = new LatLng(aLocation.getLatitude(), aLocation.getLongitude()); // marker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 1) // .icon(BitmapDescriptorFactory.fromResource(R.drawable.point)) // .position(latng).title("?:"+this.aLocation.getCity()+this.aLocation.getDistrict()+this.aLocation.getRoad())); marker.setPosition(latng); marker.setTitle("?:" + this.aLocation.getCity() + this.aLocation.getDistrict() + this.aLocation.getRoad()); marker.showInfoWindow(); } } } /** * ? */ @Override public void activate(LocationSource.OnLocationChangedListener listener) { mListener = listener; if (mAMapLocationManager == null) { mAMapLocationManager = LocationManagerProxy.getInstance(this); /* * mAMapLocManager.setGpsEnable(false); * 1.0.2true???gps?false?true Location * API?GPS??? * ??provider?2000??????? */ mAMapLocationManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000, 10, this); } } /** * ?? */ @Override public void deactivate() { mListener = null; if (mAMapLocationManager != null) { mAMapLocationManager.removeUpdates(this); mAMapLocationManager.destory(); } mAMapLocationManager = null; } //------------------------------------------------------------ //------------------------------------------------------------ //-------------------------??----------------------- //------------------------------------------------------------ //------------------------------------------------------------ //------------------------------------------------------------ }