이미 Map이 로드 된 상태에서 액티비티 안의 버튼 클릭 시, 현재 Location의 위도, 경도 값을 받아와서 마커를 찍어주려 합니다.
현재위치를 받아왔고, 마커를 추가적으로 넣고 싶은데요ㅠ
googlemap에 addMarker를 실행하면
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
라는 오류가 출력됩니다.
해당 부분 코드입니다.
public void onClick(DialogInterface dialogInterface, int i) {
FindFragment find = new FindFragment();
find.setParkingLocation(pk_latitude, pk_longtitude, search_placeCarNumber, search_placeName, search_placeNumber);
//위도, 경도값에 포인터 찍어주기
}
버튼 클릭 시 FindFragment의 setParkingLocation 메소드에 접근하도록 설정하였습니다.
public class FindFragment extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleMap googleMap;
private MapView mapView;
GoogleApiClient googleApiClient;
MarkerOptions markerOptions;
public void setCurrentLocation(Location location) {
if(location!=null){
LatLng currentLocation = new LatLng( location.getLatitude(), location.getLongitude());//현재위치의 위도 경도 가져옴
this.googleMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
return;
}
this.googleMap.moveCamera(CameraUpdateFactory.newLatLng(DEFAULT_LOCATION));
}
public void setParkingLocation(String latitude, String longtitude, String carNumber, String placeName, String placeNumber){
LatLng parkingLocation = new LatLng( Double.parseDouble(latitude), Double.parseDouble(longtitude));
markerOptions = new MarkerOptions();
markerOptions.position(parkingLocation);
markerOptions.title(carNumber + "의 위치");
markerOptions.snippet(placeName + " - " + placeNumber);
googleMap.addMarker(markerOptions); //오류 위치
}
setParkingLocation의 googleMap.addMarker(markerOptions); 이 부분에서 오류가 발생하네요.
(코드 상에서 오류 위치는 주석으로 표시해두었습니다.)
몇 시간 동안 stack overflow나 구글을 다 뒤져봤는데도 답이 안나오네요ㅠㅠ
제가 구현하려고 하는 내용은 이미 로드된 지도에서, 특정 이벤트 발생 시(버튼 클릭 시), setParkingLocation에 접근 해 googleMap에 addMarker를 진행하고 싶습니다.
혹시나 아시는 부분 있으면 코멘트 부탁드립니다.