簡體   English   中英

如何在Android中獲取谷歌地圖藍點的經度和緯度

[英]how to get the google map blue point latitude and longitude in android

我的目標是跟蹤用戶的位置並在到達標記的位置時發出警報。 所以首先我得到了到達的位置並標記了該位置。 然后,我得到當前位置並將其與標記的位置進行比較。 但這是行不通的。 甚至我將標記放置在當前位置。 它沒有給出吐司信息。 這是代碼-

public class MainActivity extends FragmentActivity implements LocationListener{
    LocationManager locationManager;
    String provider;
    ToggleButton toggle;

    public static double lat,lang,value1,value2,value3,value4;
    public static String strlat,strlat1;
    public LatLng loc,loc1;
    public  double latitude,longitude; 
    public static String strlang,strlang1 ;
    Location location;

    public LocationManager locationmanager;
        // Google Map
            private GoogleMap googleMap;
            LocationListener Locationlistener;

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

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

                // Creating an empty criteria object
                Criteria criteria = new Criteria();

                // Getting the name of the provider that meets the criteria
                provider = locationManager.getBestProvider(criteria, false);

               if(provider!=null && !provider.equals("")){

                    // Get the location from the given provider 
                    final Location location = locationManager.getLastKnownLocation(provider);

                    locationManager.requestLocationUpdates(provider, 1000, 1, this);

                    final Handler handler = new Handler();

                    Runnable runnable = new Runnable() {

                        public void run() {
                            if(location!=null){
                                lat = location.getLatitude();
                            lang = location.getLongitude();

                             value3 =Double.parseDouble(new DecimalFormat("##.##").format(lat));
                             value4 =Double.parseDouble(new DecimalFormat("##.##").format(lang));
                             strlat = String.valueOf(value3);
                                strlang = String.valueOf(value4);
                             loc1 =new LatLng(lat, lang);
                            if((value1 == value3) && (value2 == value4))
                            {
                                Toast.makeText(getApplicationContext(), "target reached", Toast.LENGTH_SHORT).show();
                            }
                            else
                                Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

                        }
                            else{
                            Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
                        }
                            handler.postDelayed(this, 1000);
                        }
                    };

                    handler.post(runnable);
               }

                try {
                    // Loading map
                    initilizeMap();

                    // Changing map type
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    // googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                    // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                    // googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                    // googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);

                    // Showing / hiding your current location

                googleMap.setMyLocationEnabled(true);

                    // Enable / Disable zooming controls
                    googleMap.getUiSettings().setZoomControlsEnabled(false);

                    // Enable / Disable my location button
                    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

                    // Enable / Disable Compass icon
                    googleMap.getUiSettings().setCompassEnabled(true);

                    // Enable / Disable Rotate gesture
                    googleMap.getUiSettings().setRotateGesturesEnabled(true);

                    // Enable / Disable zooming functionality
                    googleMap.getUiSettings().setZoomGesturesEnabled(true);

                    // create marker
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));

                } catch (Exception e) {
                    e.printStackTrace();
                }

                googleMap.setOnMapClickListener(new OnMapClickListener(){
                    public void onMapClick(LatLng point){
                        Toast.makeText(getBaseContext(),
                            point.latitude + ", " + point.longitude,
                            Toast.LENGTH_LONG).show();
                        latitude = point.latitude;
                        longitude = point.longitude;
                        value1 =Double.parseDouble(new DecimalFormat("##.##").format(latitude));
                        value2 =Double.parseDouble(new DecimalFormat("##.##").format(longitude));
                        strlat1 = String.valueOf(value1);
                        strlang1 = String.valueOf(value2);
                        loc=new LatLng(point.latitude, point.longitude);
                        MarkerOptions marker = new MarkerOptions().position(loc).title("Hello Maps ");

                        // adding marker
                        googleMap.addMarker(marker);

                    }
                });
            }

            @Override
            protected void onResume() {
                super.onResume();
                initilizeMap();
            }

            /**
             * function to load map If map is not created it will create it for you
             * */
            private void initilizeMap() {
                if (googleMap == null) {
                    googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                            R.id.map)).getMap();

                    // check if map is created successfully or not
                    if (googleMap == null) {
                        Toast.makeText(getApplicationContext(),
                                "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            }

            /*
             * creating random position around a location for testing purpose only
             */

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }
    }

您的代碼中有些錯誤。

您無法將坐標與==進行比較,因為在跟蹤設備的位置時,您永遠不會完全匹配經緯度坐標。 您需要確定一個閾值,如果設備達到該閾值,則顯示警報。

接下來,您不使用onLocationChanged()回調,因此您只是在應用啟動時查看當前位置。

看一下我的其他答案中的代碼,該代碼有關確定設備是否在圓圈內。 這是一個類似的問題。

因此,如果將閾值設置為10米,並且在設備距目標位置10米以內的情況下考慮達到的目標,則可以實現onLocationChanged()方法,如下所示:

@Override
public void onLocationChanged(Location location) {

    float[] distance = new float[2];

    Location.distanceBetween( location.getLatitude(), location.getLongitude(),
            latitude, longitude, distance);

    if( distance[0] < 10 ){
        //target location reached
        Toast.makeText(getApplicationContext(), "target reached", Toast.LENGTH_SHORT).show();
    }
}

另一個daniel.i更改了上面的代碼,現在可以正常工作了,還有更多問題。

1.i試圖像在我的second.class中那樣設置啟動屏幕。我為google map設置了上面的代碼。它在從一個移動到另一個的過程中運行了五秒鍾,但是它停止了

            new Timer().schedule(new TimerTask(){
                public void run() { 
                    Intent intent =  new Intent (MainActivity.this, SecondActivity.class);
                    PendingIntent pendIntent = PendingIntent.getActivity(getBaseContext(), 1000, intent, 0);
                }
            }, 5000);

2.我想在谷歌地圖中設置搜索,如何設置我試圖使其不工作給一些想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM