想要使用GoogleMaps - OnMyLocationChangeListener但无法实现吗?任何其他选择

时间:2014-03-13 00:45:00

标签: java android eclipse android-fragments android-activity

我想使用Google地图,OnlocationChangeListener,但因为我已经实施了

implements DatePickerFragment.OnDATEClickListener{

由于上面需要继续实现,这使得实现OnlocationChangeListener变得非常困难。此外,onLocationchangelistener代码目前会延伸fragmentActivity,而我当前的代码extends fragment会产生更多问题。

问题是:我无法扩展或实施任何其他内容,因为我已经在实施和扩展。这意味着我无法使OnlocationChangeListener工作

你知道有什么工作吗?

(允许我实施DatePickerFragment.OnDATEClickListener同时实施Google OnlocationChangeListener。同时我的代码也继续扩展片段而不是片段活动)

我当前的代码:

public class HomeFragment extends Fragment implements DatePickerFragment.OnDATEClickListener{

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        return rootView;
    }
}

Google地图位置更改侦听器代码:

    public class MainActivity extends FragmentActivity implements OnMyLocationChangeListener {

        GoogleMap googleMap;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Getting Google Play availability status
            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

            // Showing status
            if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available         
                int requestCode = 10;
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
                dialog.show();

            }else { // Google Play Services are available   

                // Getting reference to the SupportMapFragment of activity_main.xml
                SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

                // Getting GoogleMap object from the fragment
                googleMap = fm.getMap();

                // Enabling MyLocation Layer of Google Map
                googleMap.setMyLocationEnabled(true);           

                // Setting event handler for location change
                googleMap.setOnMyLocationChangeListener(this);      

            }

        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }


        @Override
        public void onMyLocationChange(Location location) {

//Location stuff

        }
    }

1 个答案:

答案 0 :(得分:2)

以下是我在示例应用中实施 Google地图的方式:

首先将支持库添加到eclipse,并确保您正在构建的应用程序包含库。

之后执行如下:

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
    }

位置已更改:

    @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;
    }

检查服务是否正常,然后继续构建地图

    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;
    }
}

最后,清单:

<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"/>

应用程序代码

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

希望这能回答你的问题.. :)