2016-05-30 95 views
0

我试图获取一个谷歌地图的当前位置,但我在如何获取当前位置的谷歌地图?

得到这个错误

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)); 
      } 
     }; 
    } 
} 

回答

0

这可能发生,因为地图是没有准备好在这一刻。

@Override 
public void onMapReady(GoogleMap googleMap) { 

//make the call here. it will be called once the map is ready 
replaceMapFragment(); 
} 
0

在回应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()); 
    } 
} 
+0

这是同样的问题。标记仍然隐藏。我的代码是否正确? –

+0

为了确保onMapReady()被调用,您需要以不同的方式声明类,请参阅我编辑的答案。之前发布不完整答案的道歉。 –

相关问题