Finding Location on Google Map

后端 未结 3 884
死守一世寂寞
死守一世寂寞 2021-01-28 13:43

i am trying to find my location on Google Map. My code is here. I have run my app in my telephone but it\'s unfortunately stopped. Any ideas about what can be the problem ? Than

3条回答
  •  -上瘾入骨i
    2021-01-28 14:09

    // try this
    **main.xml**
    
    
    
        
    
    
    
    public class MyActivity extends FragmentActivity {
    
    
        public static LocationManager mlocManager;
        public static LocationListner mListner;
        GoogleMap map;
    
        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            map = getMapView();
            map.setMyLocationEnabled(true);
    
            try {
                mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
                mListner = new LocationListner();
                runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        try {
                            try {
                                mlocManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mListner);
                            } catch (Throwable e) {
                                e.printStackTrace();
                            }
    
                            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListner);
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }
                        try {
                            mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mListner);
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }
    
                    }
                });
            } catch (Throwable e) {
                e.printStackTrace();
            }
    
        }
    
        public GoogleMap getMapView() {
            FragmentManager fm = getSupportFragmentManager();
            SupportMapFragment f = (SupportMapFragment) fm.findFragmentById(R.id.maps);
    
            // Getting GoogleMap object from the fragment
            return f.getExtendedMap();
        }
    
        public void setMapMarker(LatLng loc){
            map.addMarker(new MarkerOptions().position(loc).title("Istanbul"));
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        }
    
    
        class LocationListner implements LocationListener {
    
            @Override
            public void onLocationChanged(Location location) {
                setMapMarker(new LatLng(location.getLatitude(),location.getLongitude()));
            }
    
            @Override
            public void onProviderDisabled(String provider) {
            }
    
            @Override
            public void onProviderEnabled(String provider) {
            }
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
    
        }
    }
    

提交回复
热议问题