【问题标题】:How to Redirect my current Location in google Maps如何在谷歌地图中重定向我当前的位置
【发布时间】:2015-11-13 17:21:42
【问题描述】:

当我打开谷歌地图时如何重定向我当前的位置

这是我在 SetUpMap() 中的代码

      private void setUpMap() {
             mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
             mMap.setMyLocationEnabled(true);

还有如何更改标记?我只在我的位置得到那个蓝色圆圈。我想把它改成Pin

【问题讨论】:

  • 重定向是什么意思?是否要更改标记的位置?
  • 是的,因为当我打开谷歌地图时,它会显示地图的中心,要显示我的位置,我需要单击按钮。我不想点击按钮,我只想在打开谷歌地图时显示我的位置
  • @TaengooKim 如果有助于进一步帮助社区,请接受答案。

标签: android google-maps


【解决方案1】:
If you want to open map with your location through any activity then use this code    

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
    Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
    startActivity(intent);


If you are using Google map then using location find lat long and pass this lat long in maps object
 LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
  public void onLocationChanged(Location location) {
        TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        googleMap.addMarker(new MarkerOptions().position(latLng));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
        locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
    }

【讨论】:

    【解决方案2】:

    在您的Map 准备好后立即获取用户的当前位置,并且您需要像这样为相机设置动画

    Location location = this.mGoogleMap.getMyLocation();
    
        if (location != null) {
    
            LatLng target = new LatLng(location.getLatitude(), location.getLongitude());
            CameraPosition position = this.mGoogleMap.getCameraPosition();
    
            Builder builder = new CameraPosition.Builder();
            builder.zoom(15);
            builder.target(target);
    
            this.mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
    
          }    
    

    所以,getMyLocation() 必须获取用户的位置。使用可以使用PROVIDERS 来获取位置。您还可以使用LocationListener 使用GoogleApiClient 收听位置。请在此处发布之前阅读Getting the Last Known LocationReceiving Location Updates 的文档。

    要为您的位置提供自定义标记,可以使用 addMarker

    mGoogleMap.addMarker(new MarkerOptions()
                          .position(new LatLng(location.getLatitude(), location.getLogitude()))
                          .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    

    【讨论】:

    • 我要把这段代码放在 private void setUpMapIfNeeded() 中吗??
    • 不,你不应该这样做。一旦Map 准备就绪,您就需要获取用户位置,一旦获取位置,将相机动画到特定缩放级别并显示标记。我相信您是第一次尝试,我会鼓励您先阅读文档。我做过类似的事情,在SplashScreen 中,我在获取后使用GoogleApiClient 获取位置,我加载MapActivity 并使用自定义Map 中的获取位置Marker
    【解决方案3】:

    您可以尝试使用此代码,我们可以通过 NETWORK_PROVIDER 在谷歌地图中获取我们的位置。 NETWORK_PROVIDER 从您的 WIFI 位置或您的网络提供商公司(如(IDEA、AIRTEL、VODAPHONE)中选择您的位置。

    try {
                        if (googleMap == null) {
                            googleMap = ((MapFragment) getFragmentManager()
                                    .findFragmentById(R.id.map)).getMap();
                        }
                        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                        googleMap.setMyLocationEnabled(true);
                        LocationManager location_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        LocationListener listner = new getlatlngListner();
                        location_manager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER, 2000, 2000, listner);
                        location_manager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    

    【讨论】:

      【解决方案4】:
      private MarkerOptions mMarkerOptions = new MarkerOptions();
      mMarkerOptions.visible(true);
      mMarkerOptions.icon(BitmapDescriptorFactory.fromBitmap(Your bitmap));
      mMap.setMyLocationEnabled(true);
      mMap.getUiSettings().setMyLocationButtonEnabled(false);
      mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
                          @Override
                          public void onMyLocationChange(Location location) {
                              LatLng mPos = new LatLng(location.getLatitude(),location.getLongitude());
                              mMarkerOptions.position(mPos);
                              mMap.addMarker(mMarkerOptions);
                              mMap.moveCamera(CameraUpdateFactory.newLatLng(mPos));
                              mMap.animateCamera(CameraUpdateFactory.zoomTo(17));
      
                          }
                      });
      

      【讨论】:

      • 我是否还需要使用我私人的 void setUpMap() 中的代码
      • 如果你从位置监听器调用 void 呢?或者你需要在这个方法中初始化所有东西?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多