大佬教程收集整理的这篇文章主要介绍了Android Google Map如何检查gps位置是否在圆圈内,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public class MapaEscola extends FragmentActivity { private GoogleMap googleMap; private serializable escolas; private ProgressDialog dialog; private Circle mCircle; private Marker mMarker; @TargetApi(Build.VERSION_CODEs.ICE_CREAM_SANDWICH) @Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); setContentView(R.layout.maps); // Loading map initilizeMap(); // Changing map type googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // Showing / hiding your current LOCATIOn googleMap.setMyLOCATIOnEnabled(true); // Enable / Disable zooming controls googleMap.getUiSetTings().setZoomControlsEnabled(true); // Enable / Disable my LOCATIOn button googleMap.getUiSetTings().setMyLOCATIOnButtonEnabled(true); // Enable / Disable Compass icon googleMap.getUiSetTings().setCompassEnabled(true); // Enable / Disable Rotate gesture googleMap.getUiSetTings().setRotateGesturesEnabled(true); // Enable / Disable zooming functionality googleMap.getUiSetTings().setZoomGesturesEnabled(true); Bundle extra = geTintent().getBundleExtra("extra"); ArrayList<Escolas> objects = (ArrayList<Escolas>) extra.getserializable("array"); try { for(int i = 0; i < objects.size(); i ++) { System.out.println(" escolas " + objects.get(i).getLatitude() + " " + objects.get(i).getLongitude()); float latitude = objects.get(i).getLatitude(); float longitude = objects.get(i).getLongitude(); googleMap.moveCamera(CameraupdateFactory.newLatLngZoom(new LatLng(-23.316281,-51.155528),15)); MarkerOptions options = new MarkerOptions(); // SetTing the position of the marker options.position(new LatLng(latitude,longitudE)); googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); LatLng latLng = new LatLng(latitude,longitudE); drawMarkerWithCircle(latLng); googleMap.setOnMyLOCATIOnchangelistener(new GoogleMap.onMyLOCATIOnchangelistener() { @Override public void onMyLOCATIOnChange(LOCATIOn LOCATIOn) { float[] distance = new float[2]; LOCATIOn.distancebetween( mMarker.getPosition().latitude,mMarker.getPosition().longitude,mCircle.getCenter().latitude,mCircle.getCenter().longitude,distancE); if( distance[0] > (mCircle.geTradius() / 2) ){ Toast.makeText(getBaseContext(),"Outside",Toast.LENGTH_LONG).show(); } else { Toast.makeText(getBaseContext(),"Inside",Toast.LENGTH_LONG).show(); } } }); } } catch (Exception E) { e.printStackTrace(); } } private void drawMarkerWithCircle(LatLng position){ double radiusInMeters = 500.0; int strokeColor = 0xffff0000; //red outline int shadeColor = 0x44ff0000; //opaque red fill CircLeoptions circLeoptions = new CircLeoptions().center(position).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8); mCircle = googleMap.addCircle(circLeoptions); MarkerOptions markerOptions = new MarkerOptions().position(position); mMarker = googleMap.addMarker(markerOptions); } private void initilizeMap() { if (googleMap == null) { googleMap = ((MapFragment) getFragmentManager().findFragmentById( R.id.map)).getMap(); // check if map is created successfully or not if (googleMap == null) { Toast.makeText(getApplicationContext(),"Não foi possível carregar o mapa",Toast.LENGTH_SHORT) .show(); } } } @TargetApi(Build.VERSION_CODEs.HONEYCOMB) @Override public void onBACkPressed() { super.onBACkPressed(); finish(); } @Override public Boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main,menu); return super.onCreateOptionsMenu(menu); } @TargetApi(Build.VERSION_CODEs.HONEYCOMB) public Boolean onOptionsItemSELEcted(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: super.onBACkPressed(); finish(); return true; } return true; } @Override protected void onResume() { super.onResume(); initilizeMap(); } }
您应该使用传入onMyLOCATIOnChange()回调的位置,以便它使用您当前的位置来判断设备是否在圆圈内:
googleMap.setOnMyLOCATIOnchangelistener(new GoogleMap.onMyLOCATIOnchangelistener() { @Override public void onMyLOCATIOnChange(LOCATIOn LOCATIOn) { float[] distance = new float[2]; /* LOCATIOn.distancebetween( mMarker.getPosition().latitude,distancE); */ LOCATIOn.distancebetween( LOCATIOn.getLatitude(),LOCATIOn.getLongitude(),distancE); if( distance[0] > mCircle.geTradius() ){ Toast.makeText(getBaseContext(),"Outside,distance from center: " + distance[0] + " radius: " + mCircle.geTradius(),Toast.LENGTH_LONG).show(); } else { Toast.makeText(getBaseContext(),"Inside,Toast.LENGTH_LONG).show(); } } });
以下是我运行的全部工作示例,它是您的原始代码的缩减版本:
public class MainActivity extends ActionBarActivity { private GoogleMap googleMap; private serializable escolas; private ProgressDialog dialog; private Circle mCircle; private Marker mMarker; @TargetApi(Build.VERSION_CODEs.ICE_CREAM_SANDWICH) @Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); setContentView(R.layout.activity_main); // Loading map initilizeMap(); // Changing map type googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // Showing / hiding your current LOCATIOn googleMap.setMyLOCATIOnEnabled(true); // Enable / Disable zooming controls googleMap.getUiSetTings().setZoomControlsEnabled(true); // Enable / Disable my LOCATIOn button googleMap.getUiSetTings().setMyLOCATIOnButtonEnabled(true); // Enable / Disable Compass icon googleMap.getUiSetTings().setCompassEnabled(true); // Enable / Disable Rotate gesture googleMap.getUiSetTings().setRotateGesturesEnabled(true); // Enable / Disable zooming functionality googleMap.getUiSetTings().setZoomGesturesEnabled(true); // Bundle extra = geTintent().getBundleExtra("extra"); //ArrayList<Escolas> objects = (ArrayList<Escolas>) extra.getserializable("array"); try { //test outside double mLatitude = 37.77657; double mLongitude = -122.417506; //test inside //double mLatitude = 37.7795516; //double mLongitude = -122.39292; googleMap.moveCamera(CameraupdateFactory.newLatLngZoom(new LatLng(mLatitude,mLongitudE),15)); MarkerOptions options = new MarkerOptions(); // SetTing the position of the marker options.position(new LatLng(mLatitude,mLongitudE)); //googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); LatLng latLng = new LatLng(mLatitude,mLongitudE); drawMarkerWithCircle(latLng); googleMap.setOnMyLOCATIOnchangelistener(new GoogleMap.onMyLOCATIOnchangelistener() { @Override public void onMyLOCATIOnChange(LOCATIOn LOCATIOn) { float[] distance = new float[2]; /* LOCATIOn.distancebetween( mMarker.getPosition().latitude,distancE); */ LOCATIOn.distancebetween( LOCATIOn.getLatitude(),distancE); if( distance[0] > mCircle.geTradius() ){ Toast.makeText(getBaseContext(),Toast.LENGTH_LONG).show(); } } }); } catch (Exception E) { e.printStackTrace(); } } private void drawMarkerWithCircle(LatLng position){ double radiusInMeters = 500.0; int strokeColor = 0xffff0000; //red outline int shadeColor = 0x44ff0000; //opaque red fill CircLeoptions circLeoptions = new CircLeoptions().center(position).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8); mCircle = googleMap.addCircle(circLeoptions); MarkerOptions markerOptions = new MarkerOptions().position(position); mMarker = googleMap.addMarker(markerOptions); } private void initilizeMap() { if (googleMap == null) { googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById( R.id.map)).getMap(); // check if map is created successfully or not if (googleMap == null) { Toast.makeText(getApplicationContext(),menu); return super.onCreateOptionsMenu(menu); } @TargetApi(Build.VERSION_CODEs.HONEYCOMB) public Boolean onOptionsItemSELEcted(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: super.onBACkPressed(); finish(); return true; } return true; } @Override protected void onResume() { super.onResume(); initilizeMap(); } }
圈内结果:
圆圈外的结果:
以上是大佬教程为你收集整理的Android Google Map如何检查gps位置是否在圆圈内全部内容,希望文章能够帮你解决Android Google Map如何检查gps位置是否在圆圈内所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。