2016-05-30 6 views
0

私は現在の場所でGoogleマップを取得しようとしたが、私はマップの準備ができていないため、このエラー現在の場所でGoogleマップを取得するにはどうすればよいですか?

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

この月が起こっました現時点では。 Kasunの答えにあなたのコメントに応えて

@Override 
public void onMapReady(GoogleMap googleMap) { 

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

、呼び出し、地図上の現在位置マーカを表示するには:

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()が呼び出されるようにするには、クラスを別の方法で宣言する必要があります。前に不完全な回答を投稿することについての謝罪。 –

関連する問題