【问题标题】:How can I fetch google map with current location?如何获取当前位置的谷歌地图?
【发布时间】:2016-05-30 09:49:04
【问题描述】:

我尝试使用当前位置获取谷歌地图,但出现此错误

FATAL EXCEPTION: main java.lang.NullPointerException

map= ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

java代码

public class MapFragment extends Fragment{

    private TextView locationText;
    private TextView addressText;
    private GoogleMap map;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_map, container, false);

        locationText = (TextView) rootview.findViewById(R.id.location);
        addressText = (TextView) rootview.findViewById(R.id.address);

        //replace GOOGLE MAP fragment in this Activity

        return rootview;
    }

    public void onMapReady(GoogleMap map) {

//make the call here. it will be called once the map is ready
        replaceMapFragment();
    }
    private void replaceMapFragment() {
        map= ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        // Enable Zoom
        map.getUiSettings().setZoomGesturesEnabled(true);

        //set Map TYPE
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        //enable Current location Button
        map.setMyLocationEnabled(true);

        //set "listener" for changing my location
        map.setOnMyLocationChangeListener(myLocationChangeListener());
    }

    private GoogleMap.OnMyLocationChangeListener myLocationChangeListener() {
        return new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
                LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
                double longitude = location.getLongitude();
                double latitude = location.getLatitude();

                Marker marker;
                marker = map.addMarker(new MarkerOptions().position(loc));
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
                locationText.setText("You are at [" + longitude + " ; " + latitude + " ]");

                //get current address by invoke an AsyncTask object
                //new GetAddressTask(getActivity()).execute(String.valueOf(latitude), String.valueOf(longitude));
            }
        };
    }
}

【问题讨论】:

    标签: android gps currentlocation


    【解决方案1】:

    这可能是因为地图目前还没有准备好。

    @Override
    public void onMapReady(GoogleMap googleMap) {
    
    //make the call here. it will be called once the map is ready
    replaceMapFragment();
    }
    

    【讨论】:

      【解决方案2】:

      响应您对 Kasun 回答的评论,要在地图上显示当前位置标记,请致电:

      googleMap.setMyLocationEnabled(true);
      

      编辑

      import com.google.android.gms.maps.SupportMapFragment;
      
      public class MapFragment extends SupportMapFragment
            implements OnMapReadyCallback {
      
          @Override
          public void onCreate(Bundle inState) {
              super.onCreate(inState);
      
              // start a task for connecting to the map
              // onMapReady() will be called when it is complete
              getMapAsync(this);
          }
      
          @Override
          public void onMapReady(GoogleMap googleMap) {
              map = googleMap
      
              // Enable Zoom
              map.getUiSettings().setZoomGesturesEnabled(true);
      
              //set Map TYPE
              map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
      
              //enable Current location Button
              map.setMyLocationEnabled(true);
      
              //set "listener" for changing my location
              map.setOnMyLocationChangeListener(myLocationChangeListener());
          }
      }
      

      【讨论】:

      • 同样的问题。标记仍然隐藏。我的代码正确吗?
      • 为确保 onMapReady() 被调用,您需要以不同的方式声明该类,请参阅我编辑的答案。很抱歉之前发布了一个不完整的答案。
      猜你喜欢
      • 2021-09-11
      • 1970-01-01
      • 2017-09-27
      • 2012-11-26
      • 2012-12-22
      • 2014-02-19
      • 1970-01-01
      相关资源
      最近更新 更多