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位置是否在圆圈内所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)