【问题标题】:Having trouble to add markers on current location无法在当前位置添加标记
【发布时间】:2014-12-31 22:26:05
【问题描述】:

我使用 google maps api v2 制作了一个自定义的 android 地图。我想在用户当前位置上做一个标记。我的代码是:

// Enabling MyLocation in Google Map
    getMap().setMyLocationEnabled(true);

    // BY THIS YOU CAN CHANGE MAP TYPE
    // mGoogleMap.setMapType(mGoogleMap.MAP_TYPE_SATELLITE);

    try {
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    Currentloc = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (Currentloc != null) {
                        onLocationChanged(Currentloc);
                        latitude = Currentloc.getLatitude();
                        longitude = Currentloc.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (Currentloc == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        Currentloc = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (Currentloc != null) {
                            onLocationChanged(Currentloc);
                            latitude = Currentloc.getLatitude();
                            longitude = Currentloc.getLongitude();
                        }
                    }
                }
            }
        }

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

    markerOptions = new MarkerOptions();

    // double lat = Currentloc.getLatitude();
    // double lng = Currentloc.getLongitude();

    LatLng latLng = new LatLng(latitude, longitude);

    // Setting the position for the marker
    markerOptions.position(latLng);

    // Setting the title for the marker.
    // This will be displayed on taping the marker
    markerOptions.title("ADD IMAGE?");
    // markerOptions.snippet("ADD IMAGE?");

    // Placing a marker on the touched position
    getMap().addMarker(markerOptions);
    getMap().setOnMarkerClickListener((OnMarkerClickListener) this);

OnLocationChanged 代码是

    public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    mLatitude = location.getLatitude();
    mLongitude = location.getLongitude();
    // LatLng latLng = new LatLng(mLatitude, mLongitude);
    //markerOptions.notify();

    //getMap().addMarker(markerOptions);
    getMap().moveCamera(
            CameraUpdateFactory.newLatLng(new LatLng(
                    location.getLatitude(), location.getLongitude())));
    getMap().animateCamera(CameraUpdateFactory.zoomTo(12));

}

我的程序运行没有错误。但我的问题是这条线

getMap().setMyLocationEnabled(true);  

在用户当前位置的地图上给了我一个蓝色指针我手动在用户当前位置添加了一个标记(如上面的代码)。我的标记和蓝色指针必须是相同的位置,因为它们描述了相同的位置(相同的纬度和经度)但它们不是。运行我的代码后,输出看起来像这张图片 https://www.dropbox.com/s/argxzb1fnarzie4/Screenshot_2014-11-05-02-43-06.png?dl=0 我的问题是根据我的代码,他们需要相同的位置,但为什么不呢?如何在蓝色指针的同一位置添加标记。 提前致谢

【问题讨论】:

  • 为什么在 OnLocationChanged(Location location) //getMap().addMarker(markerOptions);

标签: android google-maps gps google-maps-markers


【解决方案1】:

我要尝试的第一件事是在 onLocationChanged() 中移动下面的代码行

  markerOptions = new MarkerOptions();

// double lat = Currentloc.getLatitude();
// double lng = Currentloc.getLongitude();

LatLng latLng = new LatLng(latitude, longitude);

// Setting the position for the marker
markerOptions.position(latLng);

// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title("ADD IMAGE?");
// markerOptions.snippet("ADD IMAGE?");

// Placing a marker on the touched position
getMap().addMarker(markerOptions);

【讨论】:

  • 我尝试过,但效果相同,没有变化@jucas
【解决方案2】:

我认为这是因为您的 currentLocation 是最后一个已知位置。看看如果将 currentLocation 设置为

会发生什么
Currentloc = getMap().getMyLocation()

【讨论】:

  • 兄弟,当我设置这个时,我的应用程序由于空指针异常而崩溃@David Zafrani
  • 您从扩展中执行调用的类是什么类?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-30
  • 2020-06-14
  • 2021-09-03
  • 2011-12-24
  • 2016-08-22
  • 1970-01-01
  • 2012-04-16
相关资源
最近更新 更多