吐司不工作

发布于 2024-11-27 14:46:28 字数 5849 浏览 1 评论 0原文

我正在使用谷歌地图,但有些吐司不起作用。谁能告诉我我的代码有什么问题吗?

提前致谢

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapController;
import com.google.android.maps.GeoPoint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

import android.location.Geocoder;
import android.location.Address;

import com.google.android.maps.Overlay;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.util.List;
import java.util.Locale;
import java.io.IOException;

import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class GPSLocatorActivity extends MapActivity {
    private MapView mapView;
    private MapController mapController;

    private LocationManager locationManager;
    private LocationListener locationListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

        locationListener = new GPSLocationListener();

        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);

        mapView = (MapView) findViewById(R.id.mapView);


        // enable Street view by default
       // mapView.setStreetView(true);

        // enable to show Satellite view
         mapView.setSatellite(true);

        // enable to show Traffic on map
        // mapView.setTraffic(true);
        mapView.setBuiltInZoomControls(true);

        mapController = mapView.getController();
        mapController.setZoom(17); 
        mapView.invalidate();

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    private class GPSLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                GeoPoint point = new GeoPoint(
                        (int) (location.getLatitude() * 1E6), 
                        (int) (location.getLongitude() * 1E6));
                /*
                //Toast.makeText(getBaseContext(), 
                         Toast.makeText(GPSLocatorActivity.this,
                        "Latitude: " + location.getLatitude() + 
                        " Longitude: " + location.getLongitude(), 
                        Toast.LENGTH_LONG).show();
                        */
                //mapController.setZoom(17);               
                mapController.animateTo(point);
               mapController.setZoom(17);

             // add marker
                MapOverlay mapOverlay = new MapOverlay();
                mapOverlay.setPointToDraw(point);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);

               String address = ConvertPointToLocation(point);
               Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

                mapView.invalidate();
            }
        }

        public String ConvertPointToLocation(GeoPoint point) {   
            String address = "";
            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(
                    point.getLatitudeE6()  / 1E6, 
                    point.getLongitudeE6() / 1E6, 1);

                if (addresses.size() > 0) {
                    for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                        address += addresses.get(0).getAddressLine(index) + " ";
                }
            }
            catch (IOException e) {                
                e.printStackTrace();
            }   

            return address;
        } 

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    class MapOverlay extends Overlay
    {
        private GeoPoint pointToDraw;

        public void setPointToDraw(GeoPoint point) {
            pointToDraw = point;
        }

        public GeoPoint getPointToDraw() {
            return pointToDraw;
        }


        @Override
        public boolean onTouchEvent(MotionEvent e, MapView mapView) {
            // TODO Auto-generated method stub
        return   super.onTouchEvent(e, mapView);
             //mapView.invalidate();
             //return false;

        }

        @Override
        public boolean onTap(GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub
            //mapView.invalidate();
            return super.onTap(p, mapView);
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);                   

            // convert point to pixels
            Point screenPts = new Point();
            mapView.getProjection().toPixels(pointToDraw, screenPts);

            // add marker
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image        
            return true;
        }

    }
}

I am using google maps but some the toast does not work. Can anyone please let me know whatd wrong with my code.

Thanks in advance

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapController;
import com.google.android.maps.GeoPoint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

import android.location.Geocoder;
import android.location.Address;

import com.google.android.maps.Overlay;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.util.List;
import java.util.Locale;
import java.io.IOException;

import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class GPSLocatorActivity extends MapActivity {
    private MapView mapView;
    private MapController mapController;

    private LocationManager locationManager;
    private LocationListener locationListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

        locationListener = new GPSLocationListener();

        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);

        mapView = (MapView) findViewById(R.id.mapView);


        // enable Street view by default
       // mapView.setStreetView(true);

        // enable to show Satellite view
         mapView.setSatellite(true);

        // enable to show Traffic on map
        // mapView.setTraffic(true);
        mapView.setBuiltInZoomControls(true);

        mapController = mapView.getController();
        mapController.setZoom(17); 
        mapView.invalidate();

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    private class GPSLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                GeoPoint point = new GeoPoint(
                        (int) (location.getLatitude() * 1E6), 
                        (int) (location.getLongitude() * 1E6));
                /*
                //Toast.makeText(getBaseContext(), 
                         Toast.makeText(GPSLocatorActivity.this,
                        "Latitude: " + location.getLatitude() + 
                        " Longitude: " + location.getLongitude(), 
                        Toast.LENGTH_LONG).show();
                        */
                //mapController.setZoom(17);               
                mapController.animateTo(point);
               mapController.setZoom(17);

             // add marker
                MapOverlay mapOverlay = new MapOverlay();
                mapOverlay.setPointToDraw(point);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);

               String address = ConvertPointToLocation(point);
               Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

                mapView.invalidate();
            }
        }

        public String ConvertPointToLocation(GeoPoint point) {   
            String address = "";
            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(
                    point.getLatitudeE6()  / 1E6, 
                    point.getLongitudeE6() / 1E6, 1);

                if (addresses.size() > 0) {
                    for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                        address += addresses.get(0).getAddressLine(index) + " ";
                }
            }
            catch (IOException e) {                
                e.printStackTrace();
            }   

            return address;
        } 

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    class MapOverlay extends Overlay
    {
        private GeoPoint pointToDraw;

        public void setPointToDraw(GeoPoint point) {
            pointToDraw = point;
        }

        public GeoPoint getPointToDraw() {
            return pointToDraw;
        }


        @Override
        public boolean onTouchEvent(MotionEvent e, MapView mapView) {
            // TODO Auto-generated method stub
        return   super.onTouchEvent(e, mapView);
             //mapView.invalidate();
             //return false;

        }

        @Override
        public boolean onTap(GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub
            //mapView.invalidate();
            return super.onTap(p, mapView);
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);                   

            // convert point to pixels
            Point screenPts = new Point();
            mapView.getProjection().toPixels(pointToDraw, screenPts);

            // add marker
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image        
            return true;
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猥︴琐丶欲为 2024-12-04 14:46:28
Toast toast=Toast.makeText(                                                                                               
                GPSLocatorActivity.this, 
                "Latitude: " + location.getLatitude() + 
                " Longitude: " + location.getLongitude(),
                Toast.LENGTH_LONG);
toast.show(); 
Toast toast=Toast.makeText(                                                                                               
                GPSLocatorActivity.this, 
                "Latitude: " + location.getLatitude() + 
                " Longitude: " + location.getLongitude(),
                Toast.LENGTH_LONG);
toast.show(); 
嘿咻 2024-12-04 14:46:28

将其添加到开头:

public class GPSLocatorActivity extends MapActivity { 
    Context mContext = this;

现在每次您想要吐司时使用:

Toast.makeText(mContext, "some_string", Toast.LENGTH_LONG).show(); 

Add this to the beginning:

public class GPSLocatorActivity extends MapActivity { 
    Context mContext = this;

Now everytime you want to make a toast Use :

Toast.makeText(mContext, "some_string", Toast.LENGTH_LONG).show(); 
ゃ懵逼小萝莉 2024-12-04 14:46:28
Toast.makeText(GPSLocatorActivity.this, 
                        "Latitude: " + location.getLatitude() + 
                        " Longitude: " + location.getLongitude(), 
                        Toast.LENGTH_LONG).show();

试试这个...

Toast.makeText(GPSLocatorActivity.this, 
                        "Latitude: " + location.getLatitude() + 
                        " Longitude: " + location.getLongitude(), 
                        Toast.LENGTH_LONG).show();

try this...

独享拥抱 2024-12-04 14:46:28
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(context, "msg", Toast.LENGTH_SHORT).show();
    }
});
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(context, "msg", Toast.LENGTH_SHORT).show();
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文