java– 提取用户当前位置并在Google地图中显示

java– 提取用户当前位置并在Google地图中显示,第1张

概述我正在开发一个包含3个活动的android应用程序,其中一个是map.我正在使用谷歌地图我想要的是:>当用户打开活动(地图)时,它向用户显示她的位置而不是坐标.>用户还应该能够通过使用引脚来定位(旋转)任何地方,我的应用必须将此引脚的坐标存储在变量中,以便稍后使用.publicclassLAc

我正在开发一个包含3个活动的android应用程序,其中一个是map.我正在使用谷歌地图

我想要的是:

>当用户打开活动(地图)时,它向用户显示她的位置而不是坐标.
>用户还应该能够通过使用引脚来定位(旋转)任何地方,我的应用必须将此引脚的坐标存储在变量中,以便稍后使用.

public class LActivity extends MapActivity {        /** Called when the activity is first created. */    MapVIEw mapVIEw;    GeoPoint geo;    MapController mapController;    LocationManager locationManager;    CustomPinpoint itemizedoverlay;    @OverrIDe    public voID onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        mapVIEw = (MapVIEw) findVIEwByID(R.ID.mapVIEw);        // create a map vIEw        //linearLayout linearLayout = (linearLayout) findVIEwByID(R.ID.main);        mapVIEw.setBuiltInZoomControls(true);        // Either satellite or 2d         mapVIEw.setSatellite(true);        mapController = mapVIEw.getController();        mapController.setZoom(14); // Zoon 1 is world vIEw        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoUpdateHandler());        Drawable drawable = this.getResources().getDrawable(R.drawable.point);        itemizedoverlay = new CustomPinpoint(drawable);        createMarker();    }    public class GeoUpdateHandler implements LocationListener {        @OverrIDe        public voID onLocationChanged(Location location) {            int lat = (int) (location.getLatitude() * 1E6);            int lng = (int) (location.getLongitude() * 1E6);            GeoPoint point = new GeoPoint(lat, lng);            createMarker();            mapController.animateto(point); // mapController.setCenter(point);        }        @OverrIDe        public voID onProvIDerDisabled(String provIDer) {        }        @OverrIDe        public voID onProvIDerEnabled(String provIDer) {        }        @OverrIDe        public voID onStatusChanged(String provIDer, int status, Bundle extras) {        }    }    private voID createMarker() {        GeoPoint p = mapVIEw.getMapCenter();        OverlayItem overlayitem = new OverlayItem(p, "", "");        itemizedoverlay.addOverlay(overlayitem);        mapVIEw.getoverlays().add(itemizedoverlay);    }    @OverrIDe    protected boolean isRoutedisplayed() {        // Todo auto-generated method stub        return false;    }}

这是CustomPinpoint类

public class CustomPinpoint extends itemizedoverlay<OverlayItem> {    static int maxnum = 3;    OverlayItem overlays[] = new OverlayItem[maxnum];    int index = 0;    boolean full = false;    CustomPinpoint itemizedoverlay;    public CustomPinpoint(Drawable defaultMarker) {        super(boundCenterBottom(defaultMarker));    }    @OverrIDe    protected OverlayItem createItem(int i) {        return overlays[i];    }    @OverrIDe    public int size() {        if (full) {            return overlays.length;        } else {            return index;        }    }    public voID addOverlay(OverlayItem overlay) {        if (index < maxnum) {            overlays[index] = overlay;        } else {            index = 0;            full = true;            overlays[index] = overlay;        }        index++;        populate();    }}

解决方法:

我已经实现了你想要的相同的东西.我正在为您提供示例代码.在这里我也在获取位置时实现了ProgressDialog.

这是获取位置的起点.我将此命名为AndroIDLocationActivity.这是您的应用启动时开始的第一个活动.

String provIDer;    public double latitude, longitude = 0;    CurrentpositionTask getCurrentLocation;    Location currentLocation;    LocationListener locationListener;    LocationManager locationManager;    private long time=0;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        try {            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);            setCriteria();            currentLocation = AndroIDLocationActivity.this.locationManager.getLastKNownLocation(provIDer);            if (currentLocation == null) {                currentLocation = new Location(provIDer);            }            time = currentLocation.getTime();            if (latitude == 0 && longitude == 0) {                latitude = currentLocation.getLatitude();                longitude = currentLocation.getLongitude();                }            Toast.makeText(AndroIDLocationActivity.this, String.valueOf(time), Toast.LENGTH_LONG).show();

如果时间不超过1分钟,则设置时间,而不是更新位置.

            if (time >= 100000) {                latitude = 0;                longitude = 0;            }        } catch (NullPointerException e) {            // Todo: handle exception            System.out.println("Null");            e.printstacktrace();        }         catch (Exception e) {            // Todo: handle exception            e.printstacktrace();        }        runAsyncTask();        }    @OverrIDe    protected voID onDestroy() {        // Todo auto-generated method stub        super.onDestroy();        locationManager.removeUpdates(locationListener);    }    public voID setCriteria() {        Criteria criteria = new Criteria();        criteria.setAccuracy(Criteria.ACCURACY_FINE);        criteria.setAltituderequired(false);        criteria.setbearingrequired(false);        criteria.setCostAllowed(true);        criteria.setPowerRequirement(Criteria.POWER_MEDIUM);        provIDer = locationManager.getBestProvIDer(criteria, true);        Toast.makeText(getApplicationContext(), "ProvIDer - " + provIDer, Toast.LENGTH_SHORT).show();        if (provIDer == null) {            provIDer = LocationManager.GPS_PROVIDER;        }    }        public voID runAsyncTask() {        // Todo auto-generated method stub        if (getCurrentLocation == null) {            getCurrentLocation = new CurrentpositionTask();            }        if (getCurrentLocation != null) {            getCurrentLocation.execute("Searching for Location");            }    }    public boolean checkConnection()    {        //ARE WE CONNECTED TO THE NET        ConnectivityManager conMgr = (ConnectivityManager) getSystemService (AndroIDLocationActivity.CONNECTIVITY_SERVICE);        if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()&& conMgr.getActiveNetworkInfo().isConnected()) {            return true;        } else {            return false;        }    }     private class CurrentpositionTask extends AsyncTask<String, VoID, VoID>    {        private ProgressDialog Dialog = new ProgressDialog(AndroIDLocationActivity.this);        private boolean flag = true;        public CurrentpositionTask() {            locationListener = new MyLocationListener();        }        @OverrIDe        protected voID onPreExecute() {            // Todo auto-generated method stub            super.onPreExecute();            try {                if (checkConnection()) {                    Dialog.setTitle("Loading");                    Dialog.setMessage("Searching for Location");                    Dialog.show();                    locationManager.requestLocationUpdates(provIDer, 0, 0, locationListener);                }                else {                    Toast.makeText(getApplicationContext(), "Internet is Not Available", Toast.LENGTH_LONG).show();                }                } catch (Exception e) {                // Todo: handle exception                e.printstacktrace();            }        }        @OverrIDe        protected VoID doInBackground(String... params) {            // Todo auto-generated method stub            while (flag) {                if (latitude !=0 && longitude != 0) {                    flag=false;                }            }            return null;        }        @OverrIDe        protected voID onPostExecute(VoID result) {            // Todo auto-generated method stub            Toast.makeText(AndroIDLocationActivity.this, "Location floats:- " + latitude + "," + longitude, Toast.LENGTH_LONG).show();            super.onPostExecute(result);            if (Dialog != null && Dialog.isShowing()) {                Dialog.dismiss();                time=0;                Intent homeIntent = new Intent(AndroIDLocationActivity.this.getApplicationContext(), HomeMenuActivity.class);

设置lat&在这里开始新的活动

            homeIntent.putExtra("lat", latitude);    homeIntent.putExtra("lng", longitude);            startActivity(homeIntent);            }            locationManager.removeUpdates(locationListener);        }    }    class MyLocationListener implements LocationListener {        @OverrIDe        public voID onLocationChanged(Location location) {            // Todo auto-generated method stub            if (location != null) {                latitude = location.getLatitude();                longitude = location.getLongitude();                }        }        @OverrIDe        public voID onProvIDerDisabled(String arg0) {            // Todo auto-generated method stub            Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT).show();        }        @OverrIDe        public voID onProvIDerEnabled(String arg0) {            // Todo auto-generated method stub            Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();        }        @OverrIDe        public voID onStatusChanged(String arg0, int arg1, Bundle arg2) {            // Todo auto-generated method stub        }    } 

这是我显示地图的功能.这是HomeMenuActivity

public static Context context;onCreate(..) {    context = getApplicationContext(); // it will be used in Itemized Overlay     latitude = getIntent().getDoubleExtra("lat", 0);//get the lat & lng     longitude = getIntent().getDoubleExtra("lng", 0);}public voID showMap() {    // Todo auto-generated method stub    try {        geoPoint = new GeoPoint((int)(latitude * 1E6),(int)(longitude * 1E6));        mapvIEw = (MapVIEw)findVIEwByID(R.ID.mapvIEw);        mapControll= mapvIEw.getController();        mapvIEw.setBuiltInZoomControls(true);        mapvIEw.setStreetVIEw(true);        mapvIEw.setTraffic(true);        mapControll.setZoom(16);        mapControll.animateto(geoPoint);        userPic = this.getResources().getDrawable(your pic....);        userPicOverlay = new Myitemizedoverlay(userPic);        OverlayItem overlayItem = new OverlayItem(geoPoint, "", null);        userPicOverlay.addOverlay(overlayItem);        mapvIEw.getoverlays().add(userPicOverlay);        //Added symbols will be displayed when map is redrawn so force redraw Now        mapvIEw.postInvalIDate();    } catch (Exception e) {        // Todo: handle exception        e.printstacktrace();    }}

itemizedoverlay类

public class Myitemizedoverlay extends itemizedoverlay<OverlayItem> {    private ArrayList<OverlayItem> myOverlays ;    public Myitemizedoverlay(Drawable defaultMarker) {        super(boundCenterBottom(defaultMarker));        myOverlays = new ArrayList<OverlayItem>();        populate();    }    public voID addOverlay(OverlayItem overlay){        myOverlays.add(overlay);        populate();    }    @OverrIDe    protected OverlayItem createItem(int i) {        return myOverlays.get(i);    }    // Removes overlay item i    public voID removeItem(int i){        myOverlays.remove(i);        populate();    }    // Returns present number of items in List    @OverrIDe    public int size() {        return myOverlays.size();    }    public voID addOverlayItem(OverlayItem overlayItem) {        myOverlays.add(overlayItem);        populate();    }    public voID addOverlayItem(int lat, int lon, String Title) {        try {            GeoPoint point = new GeoPoint(lat, lon);            OverlayItem overlayItem = new OverlayItem(point, Title, null);            addOverlayItem(overlayItem);            } catch (Exception e) {            // Todo: handle exception            e.printstacktrace();        }    }    @OverrIDe    protected boolean onTap(int index) {        // Todo auto-generated method stub        String Title = myOverlays.get(index).getTitle();        Toast.makeText(HomeMenuActivity.context, Title, Toast.LENGTH_LONG).show();        return super.onTap(index);    }}

希望这会有所帮助…..

总结

以上是内存溢出为你收集整理的java – 提取用户当前位置并在Google地图中显示全部内容,希望文章能够帮你解决java – 提取用户当前位置并在Google地图中显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100034.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存