0

我正在尝试在当前位置更改时运行一种方法。每次当前位置更改时,它都会计算距离,然后将其显示在文本视图中。

public GoogleMap googleMap;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  //  final MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);

 // 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);
        // 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, (LocationListener) this); 


        Location marker = new Location("");
        marker.setLatitude(lat);
        marker.setLongitude(long1);

        if(select==false){
            TextView t = (TextView) findViewById(R.id.tv_location);
            t.setText("Please select a marker" + lat + long1);
            Toast.makeText(this, "not selected", Toast.LENGTH_LONG).show();

        } else if(select==true){

            float distanceBetweenPoints = location.distanceTo(marker);
            TextView t = (TextView) findViewById(R.id.tv_location);
            t.setText("Distance to station is "+ distanceBetweenPoints);
            Toast.makeText(this, "Distance is "+distanceBetweenPoints, Toast.LENGTH_LONG).show();

            if(distanceBetweenPoints<1000){
            //do something
        }

    }
}

和 onInfoWindow 代码

googleMap.setOnInfoWindowClickListener(
    new OnInfoWindowClickListener(){
        public void onInfoWindowClick(Marker marker) {

            LatLng clickedMarkerLatLng = marker.getPosition();
            lat =  clickedMarkerLatLng.latitude;
            long1 =  clickedMarkerLatLng.longitude;
            String name = marker.getTitle();
            select=true;

            TextView t = (TextView) findViewById(R.id.tv_location);
            t.setText("Location of "+ name +" is "+ lat + " by " + long1);

当位置发生变化时,我将如何进行此运行?

4

1 回答 1

0

您应该将您的类声明为
public MyClassName extends Activity implements LocationListener
然后将您的代码放入onLocationChanged

于 2013-03-21T01:39:43.667 回答