【问题标题】:Can't add marker on User's Current Location Google Map V2无法在用户当前位置 Google Map V2 上添加标记
【发布时间】:2016-01-25 11:34:16
【问题描述】:

我正在尝试在 Genymotion 模拟器上获取用户的当前位置。我已经在 Genymotion 上设置了自定义 GPS 经度和纬度。每当我尝试打开 Google 地图时,当前位置都无法在其中显示。 这是我的代码 sn-p。

  googleMap = ((MapFragment) getFragmentManager().findFragmentById(
    R.id.map1)).getMap();
  googleMap.setMyLocationEnabled(true);
  LocationManager locManager = (LocationManager) context
    .getSystemService(context.LOCATION_SERVICE);
  Criteria criteria = new Criteria();
  String locProvider = locManager.getBestProvider(criteria, false);
  Location location = locManager.getLastKnownLocation(locProvider);
  Location myLocation = googleMap.getMyLocation();
  if (myLocation != null) {
   double latitude = location.getLatitude();
   double longitude = location.getLongitude();
   LatLng latLng = new LatLng(latitude, longitude);
   googleMap.addMarker(new MarkerOptions()
     .position(latLng)
     .title("rajkot")
     .icon(BitmapDescriptorFactory
       .fromResource(R.drawable.ic_launcher)));
   googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
   googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

  } else {
   Toast.makeText(context, "unable to find location", 20).show();
  }

这是我的截图。

请帮帮我,我找不到用户的当前位置

我也在真实设备上检查它不工作

【问题讨论】:

  • 你在实际设备上试过了吗?
  • 不,我不检查它。
  • 去做吧。它可能在实际设备上运行。
  • 好的,我去看看
  • 我也在真实设备上检查过它不工作。

标签: android google-maps google-maps-api-3 genymotion-gps


【解决方案1】:

试试这个。

private GoogleMap mMap; // Might be null if Google Play services APK is not available.



mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {


            @Override
            public void onMyLocationChange(Location location) {

                double latitude = location.getLatitude();
                double longitude = location.getLongitude();

                LatLng latestlatLng = new LatLng(latitude, longitude);

                Marker myself = mMap.addMarker(new MarkerOptions().position(latestlatLng).title("It's Me!"));
              //  myself.setDraggable(true);
                  mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
            }
        });

【讨论】:

  • 它为我工作。我在我的项目中使用了它。实际上它会在同一个地方做很多标记。
【解决方案2】:

也许您可以使用 fusedLocationProviderClient 对象的 getLastLocation() 方法更改 getMyLocaton() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-22
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多