【问题标题】:Converting a Location to LatLng将位置转换为 LatLng
【发布时间】:2017-12-06 10:58:52
【问题描述】:

我正在尝试在当前位置设置标记,因此我尝试将 Location 转换为 LatLng 类:

LatLng mCurrentPlace= new LatLng(location.getLatitude(),location.getLongitude());

然后我又想起了addMarker方法:

 mMap.addMarker(new MarkerOptions()
    .title(getString(R.string.default_info_title))
    .position(mCurrentPlace)
    .snippet(getString(R.string.default_info_snippet)))

但是通过“运行”启动应用程序,它就停止了。

我哪里出错了?

谢谢。

【问题讨论】:

  • 被捕了?你的意思是它崩溃了?如果是这样,logcat 上的回溯是什么?
  • 被捕了?你什么意思
  • 如果我的回答对您有用,请接受。

标签: android google-maps geolocation


【解决方案1】:
  public void moveMap(GoogleMap gMap, double latitude, double longitude) {
        Log.v(TAG, "mapMoved: " + gMap);
        LatLng latlng = new LatLng(latitude, longitude);
        CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(latlng, 6);
        gMap.addMarker(new MarkerOptions().position(latlng));
        gMap.moveCamera(cu);
    }

在你想要定位和标记的地方调用这个方法,并在 mapasync 回调方法中。

@Override
        public void onMapReady(GoogleMap googleMap) {

            MapsInitializer.initialize(context);
            gMap = googleMap;
            gMap.getUiSettings().setMapToolbarEnabled(false);
            gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            if(items.get(getLayoutPosition())!=null )
                moveMap(gMap,latitude, longitude);
        }
        public void initializeMapView() {
            if (mapView != null) {
                // Initialise the MapView
                mapView.onCreate(null);
                // Set the map ready callback to receive the GoogleMap object
                mapView.getMapAsync(this);

            }
        }

重写 onMapReadyCallback 方法并执行此操作。

onCreate()或适配器onBindViewHolder中调用initializeMapView方法

【讨论】:

    【解决方案2】:

    尝试使用LatLng.Builder

    LatLng.Builder builder = LatLng.newBuilder();
    builder.setLatitude(location.getLatitude());
    builder.setLongitude(location.getLongitude());
    latLng = builder.build();
    

    【讨论】:

      【解决方案3】:

      地图活动

      public class MapsActivity2 extends FragmentActivity implements 
      OnMapReadyCallback, GoogleMap.OnMyLocationButtonClickListener {
      
      private GoogleMap mMap;
      
      LatLng loc;
      Location location;
      private double currentLatitude = 0;
      private double currentLongitude = 0;
      LocationManager locationManager;
      
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_maps2);
          // Obtain the SupportMapFragment and get notified when the map is ready to be used.
          SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                  .findFragmentById(R.id.map);
          mapFragment.getMapAsync(this);
      
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      
      }
      
      
      /**
       * Manipulates the map once available.
       * This callback is triggered when the map is ready to be used.
       * This is where we can add markers or lines, add listeners or atLnmove the camera. In this case,
       * we just add a marker near Sydney, Australia.
                  * If Google Play services is not installed on the device, the user will be prompted to install
       * it inside the SupportMapFragment. This method will only be triggered once the user has
       * installed Google Play services and returned to the app.
                  */
          @Override
          public void onMapReady(GoogleMap googleMap) {
              mMap = googleMap;
      
      
              // Add a marker in Sydney and move the camera
      //        LatLng sydney = new LatLng(-34, 151);
      //        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker 
      in Sydney"));
      //        mMap.moveCamera(CameraUpdateFactory.newLg(sydney));
      
          if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
              // TODO: Consider calling
              //    ActivityCompat#requestPermissions
              // here to request the missing permissions, and then overriding
              //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
              //                                          int[] grantResults)
              // to handle the case where the user grants the permission. See the documentation
              // for ActivityCompat#requestPermissions for more details.
              return;
          }
          mMap.setMyLocationEnabled(true);
          mMap.setOnMyLocationButtonClickListener(MapsActivity2.this);
      
      }
      
      
      
      public double getLatitude(){
          if(location != null){
              currentLatitude = location.getLatitude();
          }
          // return latitude
          return currentLatitude;
      }
      
      public double getLongitude(){
          if(location != null){
              currentLongitude = location.getLongitude();
          }
          // return longitude
          return currentLongitude;
      }
      
      @Override
      public boolean onMyLocationButtonClick() {
          location=mMap.getMyLocation();
          currentLatitude=getLatitude();
          currentLongitude=getLongitude();
      
          LatLng currentLocation = new LatLng(currentLatitude, currentLongitude);
          mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
          mMap.addMarker(new MarkerOptions().position(currentLocation).title("Marker in Current Location"));
      
      
          return false;
      }
      }
      

      资源 XML

      <fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:map="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/map"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.atziant.parashar.gmapsapi.MapsActivity2" />
      

      【讨论】:

        猜你喜欢
        • 2015-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-16
        • 1970-01-01
        • 1970-01-01
        • 2011-03-29
        相关资源
        最近更新 更多