Want to use GoogleMaps - OnMyLocationChangeListener but can't implement it? Any other options

前端 未结 1 777
陌清茗
陌清茗 2020-12-22 09:25

I want to utilize Google Maps, OnlocationChangeListener but because I\'ve already implemented

implements DatePickerFragment.OnDATEClickListener{

<
相关标签:
1条回答
  • 2020-12-22 09:54

    Here's how I am implementing the Google Maps in my sample app :

    First : Add the support library to eclipse and make sure that the application you are building has the library included.

    After that the implementation like this :

    public class MainActivity extends FragmentActivity implements LocationListener,
            OnMapClickListener, OnMapLongClickListener {
        private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    
        GoogleMap map;
        List<Address> matches;
        TextView tvLocation;
        String addressText;
        double latitude;
        double longitude;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if (servicesOK()) {
                Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
                setContentView(R.layout.activity_main);
    
                // Getting reference to the SupportMapFragment of activity_main.xml
                SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
    
                // Getting GoogleMap object from the fragment
                map = fm.getMap();
    
                // Enabling MyLocation Layer of Google Map
                map.setMyLocationEnabled(true);
    
                // Getting LocationManager object from System Service
                // LOCATION_SERVICE
                LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    
                // Creating a criteria object to retrieve provider
                Criteria criteria = new Criteria();
    
                // Getting the name of the best provider
                String provider = locationManager.getBestProvider(criteria, true);
    
                // Getting Current Location
                Location location = locationManager.getLastKnownLocation(provider);
    
                if (location != null) {
                    onLocationChanged(location);
                }
                locationManager.requestLocationUpdates(provider, 20000, 0, this);
    
            } else {
                setContentView(R.layout.activity_main);
    
            }
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    

    For Location changed:

        @Override
        public void onLocationChanged(Location location) {
            tvLocation = (TextView) findViewById(R.id.tv_location);
    
            // Getting latitude of the current location
            latitude = location.getLatitude();
    
            // Getting longitude of the current location
            longitude = location.getLongitude();
    
            // Creating a LatLng object for the current location
            LatLng latLng = new LatLng(latitude, longitude);
    
            // Showing the current location in Google Map
            map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    
            // Zoom in the Google Map
            map.animateCamera(CameraUpdateFactory.zoomTo(15));
    
            map.addMarker(new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title(addressText));
    
            map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    
                @Override
                public boolean onMarkerClick(Marker marker) {
                    Toast.makeText(getBaseContext(), "marker clicked",
                            Toast.LENGTH_LONG).show();
                    return false;
                }
    
            });
    
            // TODO Auto-generated method stub
    
            Geocoder geoCoder = new Geocoder(this);
    
            try {
                matches = geoCoder.getFromLocation(latitude, longitude, 1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
            addressText = String.format("%s, %s, %s", bestMatch
                    .getMaxAddressLineIndex() > 0 ? bestMatch.getAddressLine(0)
                    : "", bestMatch.getLocality(), bestMatch.getCountryName());
    
        }
    
        @Override
        public void onMapLongClick(LatLng point) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onMapClick(LatLng point) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    

    Check if the services are Ok and then proceed to build the map

        public boolean servicesOK() {
    
            int isAvailable = GooglePlayServicesUtil
                    .isGooglePlayServicesAvailable(this);
    
            if (isAvailable == ConnectionResult.SUCCESS) {
    
                return true;
    
            } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
    
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                        this, GPS_ERRORDIALOG_REQUEST);
                dialog.show();
    
            } else {
    
                Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();
    
            }
            return false;
        }
    }
    

    Finally, the manifest :

    <permission
        android:name="com.mike.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    
        <uses-permission android:name="com.example.mapsexample.permission.MAPS_RECEIVE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name ="android.permission.ACCESS_NETWORK_STATE"/>
    
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true"/>
    

    And in the Application Tag :

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="Your Api" />
    

    Hope this answers your question .. :)

    0 讨论(0)
提交回复
热议问题